NH_userscript

Wrappers for dealing with variations in userscript managers.

当前为 2023-10-26 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/478349/1270856/NH_userscript.js

  1. // ==UserScript==
  2. // ==UserLibrary==
  3. // @name NH_userscript
  4. // @description Wrappers for dealing with variations in userscript managers.
  5. // @version 0
  6. // @license GPL-3.0-or-later; https://www.gnu.org/licenses/gpl-3.0-standalone.html
  7. // @homepageURL https://github.com/nexushoratio/userscripts
  8. // @supportURL https://github.com/nexushoratio/userscripts/issues
  9. // @match https://www.example.com/*
  10. // ==/UserLibrary==
  11. // @namespace https://gf.qytechs.cn/users/1139937
  12. // ==/UserScript==
  13.  
  14. window.NexusHoratio ??= {};
  15.  
  16. window.NexusHoratio.userscript = (function base() {
  17. 'use strict';
  18.  
  19. /** @type {number} - Bumped per release. */
  20. const version = 0;
  21.  
  22. /** Library specific exception. */
  23. class UserscriptError extends Error {
  24.  
  25. /** @inheritdoc */
  26. constructor(...rest) {
  27. super(...rest);
  28. this.name = this.constructor.name;
  29. }
  30.  
  31. }
  32.  
  33. /**
  34. * @typedef LicenseData
  35. * @property {string} name - License name.
  36. * @property {string} url - URL pointing to the license.
  37. */
  38.  
  39. /**
  40. * Per the *old* GM docs:
  41. * https://sourceforge.net/p/greasemonkey/wiki/Metadata_Block/#license
  42. * @returns {LicenseData} - Extracted from the userscript header.
  43. * @throws {Error} - If cannot be extracted.
  44. */
  45. function licenseData() {
  46. let license = GM.info.script.license;
  47. if (!license) {
  48. const magic = '// @license ';
  49.  
  50. // Try Tampermonkey's way.
  51. const header = GM.info.script.header;
  52. if (header) {
  53. const line = header.split('\n').find(l => l.startsWith(magic));
  54. if (line) {
  55. license = line.slice(magic.length).trim();
  56. }
  57. }
  58. }
  59.  
  60. if (!license) {
  61. const msg = [
  62. 'Unable to extract license information from the userscript.',
  63. // eslint-disable-next-line no-magic-numbers
  64. JSON.stringify(GM.info.script, null, 2),
  65. ].join('\n');
  66. throw new UserscriptError(msg);
  67. }
  68.  
  69. const [name, url] = license.split(';');
  70.  
  71. return {
  72. name: name.trim(),
  73. url: url.trim(),
  74. };
  75. }
  76.  
  77. return {
  78. version: version,
  79. UserscriptError: UserscriptError,
  80. licenseData: licenseData,
  81. };
  82.  
  83. }());

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址