NH_userscript

Wrappers for dealing with variations in userscript managers.

当前为 2023-11-22 提交的版本,查看 最新版本

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

  1. // ==UserScript==
  2. // ==UserLibrary==
  3. // @name NH_userscript
  4. // @description Wrappers for dealing with variations in userscript managers.
  5. // @version 5
  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. // ==/UserScript==
  12.  
  13. window.NexusHoratio ??= {};
  14.  
  15. window.NexusHoratio.userscript = (function userscript() {
  16. 'use strict';
  17.  
  18. /** @type {number} - Bumped per release. */
  19. const version = 5;
  20.  
  21. /** Library specific exception. */
  22. class UserscriptError extends Error {
  23.  
  24. /** @inheritdoc */
  25. constructor(...rest) {
  26. super(...rest);
  27. this.name = this.constructor.name;
  28. }
  29.  
  30. }
  31.  
  32. /**
  33. * @typedef LicenseData
  34. * @property {string} name - License name.
  35. * @property {string} url - URL pointing to the license.
  36. */
  37.  
  38. /**
  39. * Per the *old* GM docs:
  40. * https://sourceforge.net/p/greasemonkey/wiki/Metadata_Block/#license
  41. * @returns {LicenseData} - Extracted from the userscript header.
  42. * @throws {Error} - If cannot be extracted.
  43. */
  44. function licenseData() {
  45. let license = GM.info.script.license;
  46. if (!license) {
  47. const magic = '// @license ';
  48.  
  49. // Try a legacy way
  50. const header = GM.info.scriptMetaStr;
  51. if (header) {
  52. const line = header.split('\n')
  53. .find(l => l.startsWith(magic));
  54. if (line) {
  55. license = line.slice(magic.length)
  56. .trim();
  57. }
  58. }
  59. }
  60.  
  61. if (!license) {
  62. const msg = [
  63. 'Unable to extract license information from the userscript.',
  64. // eslint-disable-next-line no-magic-numbers
  65. JSON.stringify(GM.info, null, 2),
  66. ].join('\n');
  67. throw new UserscriptError(msg);
  68. }
  69.  
  70. const [name, url] = license.split(';');
  71.  
  72. return {
  73. name: name.trim(),
  74. url: url.trim(),
  75. };
  76. }
  77.  
  78. /** @returns {string[]} - Raw text about the current environment. */
  79. function environmentData() {
  80. const gm = GM.info;
  81. const msgs = [`${gm.script.name}: ${gm.script.version}`];
  82. msgs.push('NexusHoratio libraries:');
  83. for (const [lib, obj] of Object.entries(window.NexusHoratio)) {
  84. if (Object.hasOwn(obj, 'version')) {
  85. msgs.push(` ${lib}: ${obj.version}`);
  86. } else {
  87. msgs.push(` ${lib}: Unknown version`);
  88. }
  89. }
  90.  
  91. msgs.push(`Userscript manager: ${gm.scriptHandler} ${gm.version}`);
  92.  
  93. if (gm.injectInto) {
  94. msgs.push(` injected into "${gm.injectInto}"`);
  95. }
  96.  
  97. // Violentmonkey
  98. if (gm.platform) {
  99. msgs.push(`Platform: ${gm.platform.browserName} ` +
  100. `${gm.platform.browserVersion} ${gm.platform.os} ` +
  101. `${gm.platform.arch}`);
  102. }
  103.  
  104. // Tampermonkey
  105. if (gm.userAgentData) {
  106. let msg = 'Platform: ';
  107. for (const brand of gm.userAgentData.brands.values()) {
  108. msg += `${brand.brand} ${brand.version} `;
  109. }
  110. msg += `${gm.userAgentData?.platform} `;
  111. msg +=
  112. `${gm.userAgentData?.architecture}-${gm.userAgentData?.bitness}`;
  113. msgs.push(msg);
  114. }
  115. return msgs;
  116. }
  117.  
  118. /**
  119. * Fetches value from userscript storage if granted.
  120. *
  121. * Purposefully no errors if permissions are not granted
  122. *
  123. * @param {string} key - The name of the value to load.
  124. * @param {*} defaultValue - Value if not stored OR not enabled.
  125. * @returns {Promise<*>} - The value fetched or defaultValue.
  126. */
  127. function getValue(key, defaultValue) {
  128. if (GM.getValue) {
  129. return GM.getValue(key, defaultValue);
  130. }
  131. return Promise.resolve(defaultValue);
  132. }
  133.  
  134. /**
  135. * Sets a value in userscript storage if granted.
  136. *
  137. * Purposefully no errors if permissions are not granted
  138. *
  139. * @param {string} key - The name to use in the storage.
  140. * @param {*} value - The value to set.
  141. * @returns {Promise<*>} - Always resolves to null; mostly used to ensure
  142. * the write is complete.
  143. */
  144. function setValue(key, value) {
  145. if (GM.setValue) {
  146. return GM.setValue(key, value);
  147. }
  148. return Promise.resolve(null);
  149. }
  150.  
  151. return {
  152. version: version,
  153. UserscriptError: UserscriptError,
  154. licenseData: licenseData,
  155. environmentData: environmentData,
  156. getValue: getValue,
  157. setValue: setValue,
  158. };
  159.  
  160. }());

QingJ © 2025

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