Greasy Fork is available in English.

VM detector

Detector installation status for scripts on Violentmonkey.

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