common_function

为脚本提供公共执行方法

As of 2021-11-26. See the latest version.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greatest.deepsurf.us/scripts/436099/992062/common_function.js

  1. /**
  2. * 共有方法,全局共享
  3. */
  4. function commonFunction(){
  5. this.GMgetValue = function (name, value) { //得到存在本地的数据
  6. if (typeof GM_getValue === "function") {
  7. return GM_getValue(name, value);
  8. } else {
  9. return GM.getValue(name, value);
  10. }
  11. };
  12. this.GMsetValue = function(name, value){
  13. if (typeof GM_setValue === "function") {
  14. return GM_setValue(name, value);
  15. } else {
  16. return GM.setValue(name, value);
  17. }
  18. };
  19. this.GMaddStyle = function(css){
  20. var myStyle = document.createElement('style');
  21. myStyle.textContent = css;
  22. var doc = document.head || document.documentElement;
  23. doc.appendChild(myStyle);
  24. };
  25. this.GMopenInTab = function(url, open_in_background){
  26. if (typeof GM_openInTab === "function") {
  27. GM_openInTab(url, open_in_background);
  28. } else {
  29. GM.openInTab(url, open_in_background);
  30. }
  31. };
  32. this.addScript = function(url){
  33. var s = document.createElement('script');
  34. s.setAttribute('src',url);
  35. document.body.appendChild(s);
  36. };
  37. this.randomNumber = function(){
  38. return Math.ceil(Math.random()*100000000);
  39. };
  40. this.request = function(mothed, url, param){ //网络请求
  41. return new Promise(function(resolve, reject){
  42. GM_xmlhttpRequest({
  43. url: url,
  44. method: mothed,
  45. data:param,
  46. onload: function(response) {
  47. var status = response.status;
  48. var playurl = "";
  49. if(status==200||status=='200'){
  50. var responseText = response.responseText;
  51. resolve({"result":"success", "data":responseText});
  52. }else{
  53. reject({"result":"error", "data":null});
  54. }
  55. }
  56. });
  57. })
  58. };
  59. this.getCurrentTime = function(){
  60. var date = new Date();
  61. var year = date.getFullYear(); //年 ,从 Date 对象以四位数字返回年份
  62. var month = date.getMonth() + 1; //月 ,从 Date 对象返回月份 (0 ~ 11) ,date.getMonth()比实际月份少 1 个月
  63. var day = date.getDate(); //日 ,从 Date 对象返回一个月中的某一天 (1 ~ 31)
  64. var hours = date.getHours(); //小时 ,返回 Date 对象的小时 (0 ~ 23)
  65. var minutes = date.getMinutes(); //分钟 ,返回 Date 对象的分钟 (0 ~ 59)
  66. var seconds = date.getSeconds(); //秒 ,返回 Date 对象的秒数 (0 ~ 59)
  67. //修改月份格式
  68. if (month >= 1 && month <= 9) {
  69. month = "0" + month;
  70. }
  71. //修改日期格式
  72. if (day >= 0 && day <= 9) {
  73. day = "0" + day;
  74. }
  75. //修改小时格式
  76. if (hours >= 0 && hours <= 9) {
  77. hours = "0" + hours;
  78. }
  79. //修改分钟格式
  80. if (minutes >= 0 && minutes <= 9) {
  81. minutes = "0" + minutes;
  82. }
  83. //修改秒格式
  84. if (seconds >= 0 && seconds <= 9) {
  85. seconds = "0" + seconds;
  86. }
  87. //获取当前系统时间 格式(yyyy-mm-dd hh:mm:ss)
  88. var currentFormatDate = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
  89. return currentFormatDate;
  90. };
  91. this.addCommonHtmlCss = function(){
  92. var cssText =
  93. `
  94. @keyframes fadeIn {
  95. 0% {opacity: 0}
  96. 100% {opacity: 1}
  97. }
  98. @-webkit-keyframes fadeIn {
  99. 0% {opacity: 0}
  100. 100% {opacity: 1}
  101. }
  102. @-moz-keyframes fadeIn {
  103. 0% {opacity: 0}
  104. 100% {opacity: 1}
  105. }
  106. @-o-keyframes fadeIn {
  107. 0% {opacity: 0}
  108. 100% {opacity: 1}
  109. }
  110. @-ms-keyframes fadeIn {
  111. 0% {opacity: 0}
  112. 100% {opacity: 1}
  113. }
  114. @keyframes fadeOut {
  115. 0% {opacity: 1}
  116. 100% {opacity: 0}
  117. }
  118. @-webkit-keyframes fadeOut {
  119. 0% {opacity: 1}
  120. 100% {opacity: 0}
  121. }
  122. @-moz-keyframes fadeOut {
  123. 0% {opacity: 1}
  124. 100% {opacity: 0}
  125. }
  126. @-o-keyframes fadeOut {
  127. 0% {opacity: 1}
  128. 100% {opacity: 0}
  129. }
  130. @-ms-keyframes fadeOut {
  131. 0% {opacity: 1}
  132. 100% {opacity: 0}
  133. }
  134. .web-toast-kkli9{
  135. position: fixed;
  136. background: rgba(0, 0, 0, 0.7);
  137. color: #fff;
  138. font-size: 14px;
  139. line-height: 1;
  140. padding:10px;
  141. border-radius: 3px;
  142. left: 50%;
  143. transform: translateX(-50%);
  144. -webkit-transform: translateX(-50%);
  145. -moz-transform: translateX(-50%);
  146. -o-transform: translateX(-50%);
  147. -ms-transform: translateX(-50%);
  148. z-index: 9999;
  149. white-space: nowrap;
  150. }
  151. .fadeOut{
  152. animation: fadeOut .5s;
  153. }
  154. .fadeIn{
  155. animation:fadeIn .5s;
  156. }
  157. `;
  158. this.GMaddStyle(cssText);
  159. };
  160. this.webToast = function(params) { //小提示框
  161. var time = params.time;
  162. var background = params.background;
  163. var color = params.color;
  164. var position = params.position; //center-top, center-bottom
  165. var defaultMarginValue = 50;
  166. if(time == undefined || time == ''){
  167. time = 1500;
  168. }
  169. var el = document.createElement("div");
  170. el.setAttribute("class", "web-toast-kkli9");
  171. el.innerHTML = params.message;
  172. //背景颜色
  173. if(background==undefined || background==''){
  174. el.style.backgroundColor=background;
  175. }
  176. //字体颜色
  177. if(color==undefined || color==''){
  178. el.style.color=color;
  179. }
  180. //显示位置
  181. if(position==undefined || position==''){
  182. position = "center-bottom";
  183. }
  184. //设置显示位置,当前有种两种形式
  185. if(position==="center-bottom"){
  186. el.style.bottom = defaultMarginValue+"px";
  187. }else{
  188. el.style.top = defaultMarginValue+"px";
  189. }
  190. el.style.zIndex=999999;
  191. document.body.appendChild(el);
  192. el.classList.add("fadeIn");
  193. setTimeout(function () {
  194. el.classList.remove("fadeIn");
  195. el.classList.add("fadeOut");
  196. /*监听动画结束,移除提示信息元素*/
  197. el.addEventListener("animationend", function () {
  198. document.body.removeChild(el);
  199. });
  200. el.addEventListener("webkitAnimationEnd", function () {
  201. document.body.removeChild(el);
  202. });
  203. }, time);
  204. }
  205. }