Tampermonkey Support Library

to reduce repetition in creating scripts for Tampermonkey support

当前为 2018-04-17 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/23115/265708/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. waitTimers: [],
  86. getContainer: function (opts) {
  87. var options = {
  88. id: opts.id ? opts.id : opts.el.replace(/[ (:)]/g, ''),
  89. el: opts.el,
  90. try: 0,
  91. max: opts.max ? opts.max : 20,
  92. spd: opts.spd ? opts.spd : 500
  93. };
  94. clearTimeout(tm.waitTimers[options.id]);
  95. return new Promise(function (resolve, reject) {
  96. tm.waitForContainerElement(resolve, options);
  97. });
  98. },
  99. waitForContainerElement: function (resolve, options) {
  100. var $configElement = $(options.el);
  101. if ($configElement.length === 0) {
  102. options.try++;
  103. if (options.try < options.max) {
  104. tm.waitTimers[options.id] = setTimeout(tm.waitForContainerElement.bind(this, resolve, options), options.spd);
  105. } else {
  106. $('#output').val($('#output').val() + 'Maximum searches reached\n');
  107. }
  108. } else {
  109. resolve($configElement);
  110. }
  111. }
  112. };