Greasy Fork is available in English.

common_function

为脚本提供公共执行方法

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.greasyfork.org/scripts/436099/992063/common_function.js

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