Greasy Fork is available in English.

Userscript App Core

Userscript App Core For Userscript Web Apps

  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.8
  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 window.onurlchange
  40. // @grant window.close
  41. // @grant window.focus
  42. // @grant unsafeWindow
  43. // @run-at document-start
  44. // ==/UserScript==
  45.  
  46. (function __MAIN__() {
  47. 'use strict';
  48.  
  49. const CONST = {
  50. Text: {
  51. SetPassword: 'View/Set Password',
  52. SetPasswordTip: 'Set your password: '
  53. }
  54. }
  55.  
  56. main();
  57. function main() {
  58. const UAC = {
  59. grant: passFunc(GM_grant, () => GM_getValue('password', null)),
  60. check: pswd => pswd === GM_getValue('password', null),
  61. version: GM_info.script.version,
  62. provider: 'Userscript-Application-Core'
  63. };
  64. Object.freeze(UAC);
  65. Object.defineProperty(unsafeWindow, 'UAC', {
  66. value: UAC,
  67. writable: false,
  68. configurable: false,
  69. enumerable: false
  70. });
  71. unsafeWindow.dispatchEvent(new Event('uac-ready'));
  72.  
  73. GM_registerMenuCommand(CONST.Text.SetPassword, setPass);
  74. }
  75.  
  76. function setPass() {
  77. const newpass = prompt(CONST.Text.SetPasswordTip, GM_getValue('password', ''));
  78. newpass !== null && GM_setValue('password', newpass);
  79. }
  80.  
  81. function passFunc(func, pswd) {
  82. return function() {
  83. const password = typeof pswd === 'function' ? pswd() : pswd;
  84. const correct = arguments.length !== 0 && password === arguments[arguments.length-1];
  85.  
  86. return correct ? func.apply(this, Array.from(arguments).slice(0, arguments.length-1)) : null;
  87. }
  88. }
  89.  
  90. function GM_grant(name) {
  91. const GMFuncs = {
  92. // Tampermonkey provides
  93. GM_addStyle: typeof GM_addStyle === 'function' ? GM_addStyle : null,
  94. GM_addElement: typeof GM_addElement === 'function' ? GM_addElement : null,
  95. GM_deleteValue: typeof GM_deleteValue === 'function' ? GM_deleteValue : null,
  96. GM_listValues: typeof GM_listValues === 'function' ? GM_listValues : null,
  97. GM_addValueChangeListener: typeof GM_addValueChangeListener === 'function' ? GM_addValueChangeListener : null,
  98. GM_removeValueChangeListener: typeof GM_removeValueChangeListener === 'function' ? GM_removeValueChangeListener : null,
  99. GM_setValue: typeof GM_setValue === 'function' ? GM_setValue : null,
  100. GM_getValue: typeof GM_getValue === 'function' ? GM_getValue : null,
  101. GM_log: typeof GM_log === 'function' ? GM_log : null,
  102. GM_getResourceText: typeof GM_getResourceText === 'function' ? GM_getResourceText : null,
  103. GM_getResourceURL: typeof GM_getResourceURL === 'function' ? GM_getResourceURL : null,
  104. GM_registerMenuCommand: typeof GM_registerMenuCommand === 'function' ? GM_registerMenuCommand : null,
  105. GM_unregisterMenuCommand: typeof GM_unregisterMenuCommand === 'function' ? GM_unregisterMenuCommand : null,
  106. GM_openInTab: typeof GM_openInTab === 'function' ? GM_openInTab : null,
  107. GM_xmlhttpRequest: typeof GM_xmlhttpRequest === 'function' ? GM_xmlhttpRequest : null,
  108. GM_download: typeof GM_download === 'function' ? GM_download : null,
  109. GM_getTab: typeof GM_getTab === 'function' ? GM_getTab : null,
  110. GM_saveTab: typeof GM_saveTab === 'function' ? GM_saveTab : null,
  111. GM_getTabs: typeof GM_getTabs === 'function' ? GM_getTabs : null,
  112. GM_notification: typeof GM_notification === 'function' ? GM_notification : null,
  113. GM_setClipboard: typeof GM_setClipboard === 'function' ? GM_setClipboard : null,
  114. GM_info: typeof GM_info === 'object' ? GM_info : null,
  115. window: typeof window === 'object' ? window : null,
  116. unsafeWindow: typeof unsafeWindow === 'object' ? unsafeWindow : null,
  117. };
  118. if (GMFuncs.hasOwnProperty(name)) {
  119. return GMFuncs[name];
  120. } else {
  121. return null;
  122. }
  123. }
  124. })();