NH_userscript

Wrappers for dealing with variations in userscript managers.

目前為 2023-11-05 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/478349/1275719/NH_userscript.js

  1. // ==UserScript==
  2. // ==UserLibrary==
  3. // @name NH_userscript
  4. // @description Wrappers for dealing with variations in userscript managers.
  5. // @version 4
  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 = 4;
  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').find(l => l.startsWith(magic));
  53. if (line) {
  54. license = line.slice(magic.length).trim();
  55. }
  56. }
  57. }
  58.  
  59. if (!license) {
  60. const msg = [
  61. 'Unable to extract license information from the userscript.',
  62. // eslint-disable-next-line no-magic-numbers
  63. JSON.stringify(GM.info, null, 2),
  64. ].join('\n');
  65. throw new UserscriptError(msg);
  66. }
  67.  
  68. const [name, url] = license.split(';');
  69.  
  70. return {
  71. name: name.trim(),
  72. url: url.trim(),
  73. };
  74. }
  75.  
  76. /** @returns {string[]} - Raw text about the current environment. */
  77. function environmentData() {
  78. const gm = GM.info;
  79. const msgs = [`${gm.script.name}: ${gm.script.version}`];
  80. msgs.push('NexusHoratio libraries:');
  81. for (const [lib, obj] of Object.entries(window.NexusHoratio)) {
  82. if (Object.hasOwn(obj, 'version')) {
  83. msgs.push(` ${lib}: ${obj.version}`);
  84. } else {
  85. msgs.push(` ${lib}: Unknown version`);
  86. }
  87. }
  88.  
  89. msgs.push(`Userscript manager: ${gm.scriptHandler} ${gm.version}`);
  90.  
  91. if (gm.injectInto) {
  92. msgs.push(` injected into "${gm.injectInto}"`);
  93. }
  94.  
  95. // Violentmonkey
  96. if (gm.platform) {
  97. msgs.push(`Platform: ${gm.platform.browserName} ` +
  98. `${gm.platform.browserVersion} ${gm.platform.os} ` +
  99. `${gm.platform.arch}`);
  100. }
  101.  
  102. // Tampermonkey
  103. if (gm.userAgentData) {
  104. let msg = 'Platform: ';
  105. for (const brand of gm.userAgentData.brands.values()) {
  106. msg += `${brand.brand} ${brand.version} `;
  107. }
  108. msg += `${gm.userAgentData?.platform} `;
  109. msg +=
  110. `${gm.userAgentData?.architecture}-${gm.userAgentData?.bitness}`;
  111. msgs.push(msg);
  112. }
  113. return msgs;
  114. }
  115.  
  116. /**
  117. * Fetches value from userscript storage if granted.
  118. *
  119. * Purposefully no errors if permissions are not granted
  120. *
  121. * @param {string} key - The name of the value to load.
  122. * @param {*} defaultValue - Value if not stored OR not enabled.
  123. * @returns {Promise<*>} - The value fetched or defaultValue.
  124. */
  125. function getValue(key, defaultValue) {
  126. if (GM.getValue) {
  127. return GM.getValue(key, defaultValue);
  128. }
  129. return Promise.resolve(defaultValue);
  130. }
  131.  
  132. /**
  133. * Sets a value in userscript storage if granted.
  134. *
  135. * Purposefully no errors if permissions are not granted
  136. *
  137. * @param {string} key - The name to use in the storage.
  138. * @param {*} value - The value to set.
  139. * @returns {Promise<*>} - Always resolves to null; mostly used to ensure
  140. * the write is complete.
  141. */
  142. function setValue(key, value) {
  143. if (GM.setValue) {
  144. return GM.setValue(key, value);
  145. }
  146. return Promise.resolve(null);
  147. }
  148.  
  149. return {
  150. version: version,
  151. UserscriptError: UserscriptError,
  152. licenseData: licenseData,
  153. environmentData: environmentData,
  154. getValue: getValue,
  155. setValue: setValue,
  156. };
  157.  
  158. }());

QingJ © 2025

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