Tampermonkey Support Library

to reduce repetition in creating scripts for Tampermonkey support

当前为 2016-11-04 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/23115/156081/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. range = document.createRange();
  23. range.selectNode(document.getElementsByClassName(targetClass)[0]);
  24. window.getSelection().addRange(range);
  25. }
  26. } catch (err) {
  27. tm.log('Failed to select text');
  28. }
  29. },
  30. sysFriendly: function(el) {
  31. var text = $(el).text();
  32. $(el).text(text.replace(/\/|\:|\<|\>|\\|\||\*|\?/g, '-'));
  33. },
  34. ping: function (ip, callback) { // via http://jsfiddle.net/GSSCD/203/
  35. if (!this.inUse) {
  36. this.status = 'unchecked';
  37. this.inUse = true;
  38. this.callback = callback;
  39. this.ip = ip;
  40. var _that = this;
  41. this.img = new Image();
  42.  
  43. this.img.onload = function () {
  44. _that.inUse = false;
  45. _that.callback('responded');
  46. };
  47.  
  48. this.img.onerror = function (e) {
  49. if (_that.inUse) {
  50. _that.inUse = false;
  51. _that.callback('error', e);
  52. }
  53. };
  54. this.start = new Date().getTime();
  55. this.img.src = "http://" + ip;
  56. this.timer = setTimeout(function () {
  57. if (_that.inUse) {
  58. _that.inUse = false;
  59. _that.callback('timeout');
  60. }
  61. }, 1500);
  62. }
  63. },
  64. isTagIn: function (targetString, tags) {
  65. var isFound = false;
  66. _.each(tags, function scanTarget (tag) {
  67. if (targetString.toLowerCase().indexOf(tag.toLowerCase()) > -1) {
  68. isFound = true;
  69. }
  70. });
  71. return isFound;
  72. },
  73. copyTextToClipboard: function (text) {
  74. var copyFrom = document.createElement("textarea");
  75. copyFrom.textContent = text;
  76. var body = document.getElementsByTagName('body')[0];
  77. body.appendChild(copyFrom);
  78. copyFrom.select();
  79. document.execCommand('copy');
  80. body.removeChild(copyFrom);
  81. },
  82. waitFor: function (options) {
  83. var awaitThis = options.condition,
  84. doThis = options.resolution,
  85. intervalSpeed = options.interval ? options.interval : 100,
  86. maxTries = options.attempts ? options.attempts : 100,
  87. currentTry = 0;
  88. console.log(intervalSpeed + ' | ' + maxTries);
  89. stateLoading = function (awaitCondition) {
  90. if( !awaitCondition ) {
  91. currentTry++;
  92. if (currentTry < maxTries) {
  93. window.setTimeout(stateLoading(awaitCondition), intervalSpeed); /* this checks the flag every intervalSpeed milliseconds*/
  94. } else {
  95. console.log('final try');
  96. }
  97. } else {
  98. console.log('good');
  99. doThis();
  100. }
  101. };
  102. stateLoading(awaitThis);
  103. }
  104. };