暴力猴嗅探器

检测脚本在暴力猴上的安装状态。

当前为 2017-06-11 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name VM detector
  3. // @name:zh-CN 暴力猴嗅探器
  4. // @namespace https://violentmonkey.github.io
  5. // @description Detector installation status for scripts on Violentmonkey.
  6. // @description:zh-CN 检测脚本在暴力猴上的安装状态。
  7. // @version 1.0
  8. // @author Gerald <i@gerald.top>
  9. // @match https://greasyfork.org/*
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. !function () {
  14. const warn = message => {
  15. console.warn('[VM detector]', message);
  16. };
  17. if (GM_info.scriptHandler !== 'Violentmonkey') {
  18. warn('This script only works for Violentmonkey.');
  19. return;
  20. }
  21. if (!external.Violentmonkey) {
  22. warn('This script requires Violentmonkey 2.6.4+.');
  23. return;
  24. }
  25.  
  26. const $ = selector => document.querySelector(selector);
  27. const button = $('.install-link');
  28. if (!button) return;
  29.  
  30. const name = button.dataset.scriptName;
  31. const namespace = button.dataset.scriptNamespace;
  32. const version = button.dataset.scriptVersion;
  33. external.Violentmonkey.isInstalled(name, namespace)
  34. .then(result => {
  35. if (result) {
  36. const compare = compareVersions(result, version);
  37. if (compare < 0) {
  38. button.textContent = button.dataset.updateLabel;
  39. } else if (compare > 0) {
  40. button.textContent = button.dataset.downgradeLabel;
  41. } else {
  42. button.textContent = button.dataset.reinstallLabel;
  43. }
  44. }
  45. });
  46. }();
  47.  
  48. function compareVersions(a, b) {
  49. const va = a.split('.').map(i => +i);
  50. const vb = b.split('.').map(i => +i);
  51. for (let i = 0; i < va.length || i < vb.length; i++) {
  52. const ua = va[i];
  53. const ub = vb[i];
  54. if ((ua || ub) && (ua !== ub)) {
  55. return ua && (!ub || ua > ub) ? 1 : -1;
  56. }
  57. }
  58. return 0;
  59. }