Tampermonkey Support Library

to reduce repetition in creating scripts for Tampermonkey support

当前为 2019-06-19 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/23115/710488/Tampermonkey%20Support%20Library.js

  1. var tm = {
  2. addGlobalStyle: function(css) {
  3. var head, style;
  4. head = document.getElementsByTagName('head')[0];
  5. if (!head) { return; }
  6. style = document.createElement('style');
  7. style.type = 'text/css';
  8. style.innerHTML = css;
  9. head.appendChild(style);
  10. },
  11. log: function(msg) {
  12. console.log('Tampermonkey: ' + msg);
  13. },
  14. selectText: function (targetClass) {
  15. var textToCopy, range;
  16. try {
  17. if (document.selection) {
  18. range = document.body.createTextRange();
  19. range.moveToElementText(document.getElementsByClassName(targetClass)[0]);
  20. range.select();
  21. } else if (window.getSelection) {
  22. var selection = window.getSelection();
  23. range = document.createRange();
  24. range.selectNode(document.getElementsByClassName(targetClass)[0]);
  25. selection.removeAllRanges();
  26. selection.addRange(range);
  27. }
  28. } catch (err) {
  29. tm.log('Failed to select text');
  30. }
  31. },
  32. sysFriendly: function(el) {
  33. var text = $(el).text();
  34. $(el).text(text.replace(/\/|\:|\<|\>|\\|\||\*|\?/g, '-'));
  35. },
  36. ping: function (ip, callback) { // via http://jsfiddle.net/GSSCD/203/
  37. if (!this.inUse) {
  38. this.status = 'unchecked';
  39. this.inUse = true;
  40. this.callback = callback;
  41. this.ip = ip;
  42. var _that = this;
  43. this.img = new Image();
  44.  
  45. this.img.onload = function () {
  46. _that.inUse = false;
  47. _that.callback('responded');
  48. };
  49.  
  50. this.img.onerror = function (e) {
  51. if (_that.inUse) {
  52. _that.inUse = false;
  53. _that.callback('error', e);
  54. }
  55. };
  56.  
  57. this.start = new Date().getTime();
  58. this.img.src = "http://" + this.ip;
  59. this.timer = setTimeout(function () {
  60. if (_that.inUse) {
  61. _that.inUse = false;
  62. _that.callback('timeout');
  63. }
  64. }, 1500);
  65. }
  66. },
  67. isTagIn: function (targetString, tags) {
  68. var isFound = false;
  69. _.each(tags, function scanTarget (tag) {
  70. if (targetString.toLowerCase().indexOf(tag.toLowerCase()) > -1) {
  71. isFound = true;
  72. }
  73. });
  74. return isFound;
  75. },
  76. copyTextToClipboard: function (text) {
  77. var copyFrom = document.createElement("textarea");
  78. copyFrom.textContent = text;
  79. var body = document.getElementsByTagName('body')[0];
  80. body.appendChild(copyFrom);
  81. copyFrom.select();
  82. document.execCommand('copy');
  83. body.removeChild(copyFrom);
  84. },
  85. showModal: function(modalId, modalBody) {
  86. if ($('#' + modalId).is(":visible")) {
  87. $('#' + modalId).remove();
  88. } else {
  89. $('body').append('<div class="popupDetailWindow" id="' + modalId + '">' +
  90. ' <div class="popupDetailTitle">&nbsp;</div>' +
  91. ' <div class="popupDetailContent fingery" style="text-align:right;" onclick="$(this).parent().remove()">[CLOSE]</div>' +
  92. ' ' + modalBody +
  93. '</div>');
  94. }
  95. },
  96. waitTimers: [],
  97. getContainer: function (opts) {
  98. var options = {
  99. id: opts.id ? opts.id : opts.el.replace(/[ (:)]/g, ''),
  100. el: opts.el,
  101. try: 0,
  102. max: opts.max ? opts.max : 20,
  103. spd: opts.spd ? opts.spd : 500
  104. };
  105. clearTimeout(tm.waitTimers[options.id]);
  106. return new Promise(function (resolve, reject) {
  107. tm.waitForContainerElement(resolve, options);
  108. });
  109. },
  110. waitForContainerElement: function (resolve, options) {
  111. var $configElement = $(options.el);
  112. if ($configElement.length === 0) {
  113. options.try++;
  114. if (options.try < options.max) {
  115. tm.waitTimers[options.id] = setTimeout(tm.waitForContainerElement.bind(this, resolve, options), options.spd);
  116. } else {
  117. $('#output').val($('#output').val() + 'Maximum searches reached\n');
  118. }
  119. } else {
  120. resolve($configElement);
  121. }
  122. },
  123. savePreferences: function (name, value) {
  124. GM_setValue(name, JSON.stringify(value));
  125. },
  126. erasePreferences: function(name) {
  127. GM_setValue(name, JSON.stringify({}));
  128. },
  129. setTamperIcon: function (global) {
  130. var scriptName = global.scriptName,
  131. prefsName = global.prefsName,
  132. prefs = global.prefs,
  133. notes = global.notes;
  134. if (!scriptName || !prefsName || !prefs) {
  135. tm.log('setTamperIcon not properly configured; please send entire global object');
  136. return;
  137. }
  138. // Add Tampermonkey Icon with label to identify this script
  139. if($('.tamperlabel').length > 0) {
  140. if ($('.tamperlabel').prop('title').indexOf(scriptName) === -1) {
  141. $('.tamperlabel').prop('title', $('.tamperlabel').prop('title') + ' | ' + scriptName);
  142. }
  143. } else {
  144. $('body').append('<span class="tamperlabel" title="Tampermonkey scripts: ' + scriptName + '"></span>');
  145. }
  146. if (prefsName != null && prefs != null) {
  147. var tamperAction = function () {
  148.  
  149. var modalId = scriptName.replace(/\s/g, '') + 'Options',
  150. modalBody = '';
  151. _.each(prefs, function (value, key) {
  152. if (Array.isArray(value) || typeof value === 'string' || typeof value === 'number') {
  153. modalBody += ' <div class="popupDetailTitle">' + key + '</div><div class="popupDetailContent"><input style="width:100%" id="' + key + '" type="text" value="' + value + '"></input></div>';
  154. } else {
  155. _.each(value, function (value2, key2) {
  156. modalBody += ' <div class="popupDetailTitle">' + key2 + '</div><div class="popupDetailContent"><input style="width:100%" id="' + key2 + '" type="text" value="' + value2 + '"></input></div>';
  157. });
  158. }
  159. });
  160. if (notes != null) {
  161. modalBody += ' <div class="popupDetailTitle">&nbsp;</div><div class="popupDetailContent" style="margin-top:20px;">&nbsp;</div>';
  162. _.each(notes.messages, function(note) {
  163. modalBody += ' <div class="popupDetailTitle">NOTE:</div><div class="popupDetailContent">' + note + '</div>';
  164. });
  165. }
  166. modalBody += '<div class="popupDetailTitle">&nbsp;</div><div class="popupDetailContent" style="text-align:right;">' +
  167. ' <button class="savery">Save</button>' +
  168. ' <button class="resetery">Reset</button>' +
  169. ' <button class="uiClosify">Close</button>' +
  170. '</div>';
  171. tm.showModal(modalId, modalBody);
  172.  
  173. // hide the default popup Close because for some weird reason it's not working
  174. $('.popupDetailContent.fingery').hide();
  175.  
  176. $('.savery').on('click', function() {
  177. _.each(prefs, function(value, key) {
  178. prefs[key] = $('#' + key).val();
  179. });
  180. tm.savePreferences(prefsName, prefs);
  181. alert('Refresh to see new values.');
  182. });
  183. $('.resetery').on('click', function() {
  184. tm.erasePreferences(prefsName);
  185. alert('Refresh to see default values.');
  186. });
  187. $('.uiClosify').on('click', function() {
  188. $('#' + modalId).remove();
  189. });
  190.  
  191. return false;
  192. };
  193. $('.tamperlabel').unbind('click').click(tamperAction);
  194. }
  195. },
  196. checkNotes: function(global) {
  197. if (global.notes == null) {
  198. global.notes = {
  199. messages: [],
  200. notifiedCount: 0
  201. };
  202. }
  203. if (global.notes.messages.length !== global.notes.notifiedCount) {
  204. global.notes.notifiedCount = global.notes.messages.length;
  205. var blinkNotify = function() {
  206. if ($('.tamperlabel').css('background-color') === 'rgb(255, 0, 0)') {
  207. $('.tamperlabel').css('background-color', 'transparent');
  208. } else {
  209. $('.tamperlabel').css('background-color', 'rgb(255, 0, 0)');
  210. }
  211. }
  212. setTimeout(function() {
  213. for (var intI = 0; intI < 4; intI ++) {
  214. setTimeout(blinkNotify, 1000 * intI);
  215. };
  216. }, 3000);
  217. }
  218. }
  219. };