Userscript App Core

Userscript App Core For Userscript Web Apps

2023-12-07 يوللانغان نەشرى. ئەڭ يېڭى نەشرىنى كۆرۈش.

  1. /* eslint-disable no-multi-spaces */
  2.  
  3. // ==UserScript==
  4. // @name Userscript App Core
  5. // @name:zh-CN 用户脚本应用核心
  6. // @name:en Userscript App Core
  7. // @namespace Userscript-App
  8. // @version 0.6.1
  9. // @description Userscript App Core For Userscript Web Apps
  10. // @description:zh-CN 用户脚本网页应用核心
  11. // @description:en Userscript App Core For Userscript Web Apps
  12. // @author PY-DNG
  13. // @license MIT
  14. // @match http*://*/*
  15. // @connect *
  16. // @grant GM_info
  17. // @grant GM_addStyle
  18. // @grant GM_addElement
  19. // @grant GM_deleteValue
  20. // @grant GM_listValues
  21. // @grant GM_addValueChangeListener
  22. // @grant GM_removeValueChangeListener
  23. // @grant GM_setValue
  24. // @grant GM_getValue
  25. // @grant GM_log
  26. // @grant GM_getResourceText
  27. // @grant GM_getResourceURL
  28. // @grant GM_registerMenuCommand
  29. // @grant GM_unregisterMenuCommand
  30. // @grant GM_openInTab
  31. // @grant GM_xmlhttpRequest
  32. // @grant GM_download
  33. // @grant GM_getTab
  34. // @grant GM_saveTab
  35. // @grant GM_getTabs
  36. // @grant GM_notification
  37. // @grant GM_setClipboard
  38. // @grant GM_info
  39. // @grant unsafeWindow
  40. // @run-at document-start
  41. // ==/UserScript==
  42.  
  43. (function __MAIN__() {
  44. 'use strict';
  45.  
  46. const CONST = {
  47. Text: {
  48. SetPassword: 'View/Set Password',
  49. SetPasswordTip: 'Set your password: '
  50. }
  51. }
  52.  
  53. main();
  54. function main() {
  55. const UAC = {
  56. grant: passFunc(GM_grant, () => GM_getValue('password', null)),
  57. check: pswd => pswd === GM_getValue('password', null),
  58. version: GM_info.script.version
  59. };
  60. Object.freeze(UAC);
  61. Object.defineProperty(unsafeWindow, 'UAC', {
  62. value: UAC,
  63. writable: false,
  64. configurable: false,
  65. enumerable: false
  66. });
  67. unsafeWindow.dispatchEvent(new Event('uac-ready'));
  68.  
  69. GM_registerMenuCommand(CONST.Text.SetPassword, setPass);
  70. }
  71.  
  72. function setPass() {
  73. const newpass = prompt(CONST.Text.SetPasswordTip, GM_getValue('password', ''));
  74. newpass !== null && GM_setValue('password', newpass);
  75. }
  76.  
  77. function passFunc(func, pswd) {
  78. return function() {
  79. const password = typeof pswd === 'function' ? pswd() : pswd;
  80. const correct = arguments.length !== 0 && password === arguments[arguments.length-1];
  81.  
  82. return correct ? func.apply(this, Array.from(arguments).slice(0, arguments.length-1)) : null;
  83. }
  84. }
  85.  
  86. function GM_grant(name) {
  87. const GMFuncs = {
  88. // Tampermonkey provides
  89. GM_addStyle: typeof GM_addStyle === 'function' ? GM_addStyle : null,
  90. GM_addElement: typeof GM_addElement === 'function' ? GM_addElement : null,
  91. GM_deleteValue: typeof GM_deleteValue === 'function' ? GM_deleteValue : null,
  92. GM_listValues: typeof GM_listValues === 'function' ? GM_listValues : null,
  93. GM_addValueChangeListener: typeof GM_addValueChangeListener === 'function' ? GM_addValueChangeListener : null,
  94. GM_removeValueChangeListener: typeof GM_removeValueChangeListener === 'function' ? GM_removeValueChangeListener : null,
  95. GM_setValue: typeof GM_setValue === 'function' ? GM_setValue : null,
  96. GM_getValue: typeof GM_getValue === 'function' ? GM_getValue : null,
  97. GM_log: typeof GM_log === 'function' ? GM_log : null,
  98. GM_getResourceText: typeof GM_getResourceText === 'function' ? GM_getResourceText : null,
  99. GM_getResourceURL: typeof GM_getResourceURL === 'function' ? GM_getResourceURL : null,
  100. GM_registerMenuCommand: typeof GM_registerMenuCommand === 'function' ? GM_registerMenuCommand : null,
  101. GM_unregisterMenuCommand: typeof GM_unregisterMenuCommand === 'function' ? GM_unregisterMenuCommand : null,
  102. GM_openInTab: typeof GM_openInTab === 'function' ? GM_openInTab : null,
  103. GM_xmlhttpRequest: typeof GM_xmlhttpRequest === 'function' ? GM_xmlhttpRequest : null,
  104. GM_download: typeof GM_download === 'function' ? GM_download : null,
  105. GM_getTab: typeof GM_getTab === 'function' ? GM_getTab : null,
  106. GM_saveTab: typeof GM_saveTab === 'function' ? GM_saveTab : null,
  107. GM_getTabs: typeof GM_getTabs === 'function' ? GM_getTabs : null,
  108. GM_notification: typeof GM_notification === 'function' ? GM_notification : null,
  109. GM_setClipboard: typeof GM_setClipboard === 'function' ? GM_setClipboard : null,
  110. GM_info: typeof GM_info === 'object' ? GM_info : null,
  111. };
  112. if (GMFuncs.hasOwnProperty(name)) {
  113. return GMFuncs[name];
  114. } else {
  115. return null;
  116. }
  117. }
  118. })();