Greasy Fork is available in English.

CommonlyMainFunctions

Commonly used functions

Dit script moet niet direct worden geïnstalleerd - het is een bibliotheek voor andere scripts om op te nemen met de meta-richtlijn // @require https://update.greasyfork.org/scripts/447371/1066681/CommonlyMainFunctions.js

  1. let isDebug = true;
  2. let os = function () {
  3. var ua = navigator.userAgent, //获取浏览器UA
  4. isWindowsPhone = /(?:Windows Phone)/.test(ua),
  5. isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone,
  6. isAndroid = /(?:Android)/.test(ua),
  7. isFireFox = /(?:Firefox)/.test(ua),
  8. isChrome = /(?:Chrome|CriOS)/.test(ua),
  9. isTablet = /(?:iPad|PlayBook)/.test(ua) || (isAndroid && !/(?:Mobile)/.test(ua)) || (isFireFox && /(?:Tablet)/.test(ua)),
  10. isPhone = /(?:iPhone)/.test(ua) && !isTablet,
  11. isPc = !isPhone && !isAndroid && !isSymbian;
  12. return {
  13. isTablet: isTablet,
  14. isPhone: isPhone,
  15. isAndroid: isAndroid,
  16. isPc: isPc
  17. };
  18. }();
  19. function Get(link) {
  20. return new Promise(function (resolve) {
  21. $.get(link, data => {
  22. resolve(data);
  23. });
  24. });
  25. }
  26. //日志
  27. function log() {
  28. if (isDebug) {
  29. console.log.apply(this, arguments);
  30. }
  31. };
  32. function err() {
  33. if (isDebug) {
  34. console.error.apply(this, arguments);
  35. }
  36. }
  37. function addStyle(statement = null, href = null) {
  38. let mountElement = document.getElementsByTagName('head')[0];
  39. if (mountElement) {
  40. let style = null;
  41. if (href !== null) {
  42. style = document.createElement('link');
  43. style.setAttribute('href', href);
  44. style.setAttribute('rel', 'stylesheet');
  45. } else if (statement !== null) {
  46. style = document.createElement('style');
  47. style.setAttribute('type', 'text/css');
  48. style.textContent = statement;
  49. }
  50. return new Promise((resolve, reject) => {
  51. try {
  52. mountElement.appendChild(style);
  53. style.onerror = (e) => reject(e);
  54. style.onload = () => {
  55. resolve();
  56. }
  57. } catch (error) {
  58. reject(error);
  59. }
  60. });
  61. }
  62. return null;
  63. }
  64. function addScript(statement = null, src = null, isModule = false) {
  65. let mountElement = document.getElementsByTagName('head')[0];
  66. if (mountElement) {
  67. let script = document.createElement("script");
  68. if (src !== null) {
  69. script.src = src;
  70. } else if (statement !== null) {
  71. script.textContent = statement;
  72. if (isModule) script.type = "module";
  73. }
  74. return new Promise((resolve, reject) => {
  75. try {
  76. mountElement.appendChild(script);
  77. script.onerror = (e) => reject(e);
  78. script.onload = () => {
  79. resolve();
  80. }
  81. } catch (error) {
  82. reject(error);
  83. }
  84. });
  85. }
  86. return null;
  87. }
  88. //判断变量是否为空 空返回TRUE 否则返回 FALSE
  89. function isEmpty(param){
  90. if(param){
  91. var param_type = typeof(param);
  92. if(param_type == 'object'){
  93. //要判断的是【对象】或【数组】或【null】等
  94. if(typeof(param.length) == 'undefined'){
  95. if(JSON.stringify(param) == "{}"){
  96. return true;//空值,空对象
  97. }
  98. }else if(param.length == 0){
  99. return true;//空值,空数组
  100. }
  101. }else if(param_type == 'string'){
  102. //如果要过滤空格等字符
  103. var new_param = param.trim();
  104. if(new_param.length == 0){
  105. //空值,例如:带有空格的字符串" "。
  106. return true;
  107. }
  108. }else if(param_type == 'boolean'){
  109. if(!param){
  110. return true;
  111. }
  112. }else if(param_type== 'number'){
  113. if(!param){
  114. return true;
  115. }
  116. }
  117. return false;//非空值
  118. }else{
  119. //空值,例如:
  120. //(1)null
  121. //(2)可能使用了js的内置的名称,例如:var name=[],这个打印类型是字符串类型。
  122. //(3)空字符串''、""。
  123. //(4)数字0、00等,如果可以只输入0,则需要另外判断。
  124. return true;
  125. }
  126. }
  127. //自定义过滤函数
  128. function trimSpace(array) {
  129. for (var i = 0; i < array.length; i++) {
  130. //这里为过滤的值
  131. if (array[i] == "" || array[i] == null || typeof (array[i]) == "undefined") {
  132. array.splice(i, 1);
  133. i = i - 1;
  134. }
  135. }
  136. return array;
  137. }