VM detector

Detector for scripts on Violentmonkey.

目前为 2017-06-07 提交的版本。查看 最新版本

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