├── README.md └── vm-detector.user.js /README.md: -------------------------------------------------------------------------------- 1 | VM detector 2 | --- 3 | 4 | Detect installation status for scripts on Violentmonkey. 5 | 6 | This is a userscript to work with Violentmonkey 2.6.4+. 7 | 8 | It changes the label of install button on userscript sites according to version of currently installed script, i.e. if there is an update, the label may be something like `Upgrade to v2.x.x`. 9 | 10 | Links: 11 | - GitHub: 12 | - Greasyfork: 13 | 14 | Supported sites: 15 | - 16 | 17 | ### Snapshots 18 | - before using this script: 19 | 20 | ![before](https://user-images.githubusercontent.com/3139113/26868463-b051bf64-4b9b-11e7-94d7-6313be7ec259.png) 21 | 22 | - after using this script: 23 | 24 | ![after](https://user-images.githubusercontent.com/3139113/26868474-b8655080-4b9b-11e7-9042-eb6561c57425.png) 25 | -------------------------------------------------------------------------------- /vm-detector.user.js: -------------------------------------------------------------------------------- 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 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 | } 69 | --------------------------------------------------------------------------------