Userscript Console (For developers)

Simple script that provides window.GM_Exec that can access GM_*functions

  1. // ==UserScript==
  2. // @name Userscript Console (For developers)
  3. // @namespace Userscript Console
  4. // @version 0.1
  5. // @description Simple script that provides window.GM_Exec that can access GM_*functions
  6. // @author PY-DNG
  7. // @include *
  8. // @connect *
  9. // @grant unsafeWindow
  10. // @grant GM_setValue
  11. // @grant GM_getValue
  12. // @grant GM_deleteValue
  13. // @grant GM_listValues
  14. // @grant GM_xmlhttpRequest
  15. // @grant GM_addStyle
  16. // @grant GM_addElement
  17. // @grant GM_addValueChangeListener
  18. // @grant GM_removeValueChangeListener
  19. // @grant GM_log
  20. // @grant GM_getResourceText
  21. // @grant GM_getResourceURL
  22. // @grant GM_registerMenuCommand
  23. // @grant GM_unregisterMenuCommand
  24. // @grant GM_openInTab
  25. // @grant GM_download
  26. // @grant GM_getTab
  27. // @grant GM_saveTab
  28. // @grant GM_getTabs
  29. // @grant GM_notification
  30. // @grant GM_setClipboard
  31. // @grant GM_info
  32. // ==/UserScript==
  33.  
  34. (function() {
  35. 'use strict';
  36.  
  37. // Set a password and you will get access to use GM_Exec in browser's console
  38. // password should be a random string that is impossible to be guessed by anyone else, and should be longer than 4 characters.
  39. // This aims to prevent the fucking crackers attacking you from their website.
  40. let password = 'pswd';
  41.  
  42. /* Usage:
  43. ** window.GM_Exec(code, pswd): the same as GM_Exec
  44. ** GM_Exec(code, pswd):
  45. ** - code: The code you want to execute in Tempermonkey Sandbox
  46. ** - pswd: The password you set above
  47. ** Remember:
  48. ** Calling GM_Exec with wrong password has no effect but to increase the number variable <triedCount>, your code will not be executed.
  49. ** For safety, you have to wait at least 1 second before you call GM_Exec again after you called it with a wrong password
  50. ** GM_Exec will be deleted if triedCount has reached a big number
  51. ** All these above is for safety, protecting your password from being tested out or guessed from the fucking crackers attacking you from their website
  52. */
  53.  
  54.  
  55.  
  56. // Code
  57. let lastTry;
  58. let triedCount = 0;
  59. unsafeWindow.GM_Exec = function(code, pswd) {
  60. // Password verify
  61. if (typeof(password) !== 'string') {
  62. GM_log('Password should be a string!');
  63. return;
  64. }
  65. if (password.length === 0) {
  66. GM_log('You should set a password first!');
  67. return;
  68. }
  69. if (password.length < 4) {
  70. GM_log('Password should be equal or longer than 4 characters!');
  71. return;
  72. }
  73. if (!pswd) {
  74. GM_log('You should call GM_Exec with your password as the second argument!');
  75. return;
  76. }
  77. if ((((new Date()).getTime() - lastTry) / 1000) < 1) {
  78. GM_log('You should wait at least 1 second before you call GM_Exec again after you called it with a wrong password.');
  79. lastTry = (new Date()).getTime();
  80. triedCount++;
  81. return;
  82. }
  83. if (triedCount >= 10 ** (password.length-2)) {
  84. if (unsafeWindow.GM_Exec) {
  85. delete unsafeWindow.GM_Exec;
  86. GM_log('You have tried too many times with wrong passwords, and GM_Exec is now deleted. Please reload.');
  87. }
  88. return;
  89. }
  90. if (pswd !== password) {
  91. lastTry = (new Date()).getTime();
  92. triedCount++;
  93. GM_log('Wrong password! Your code will not be executed. Try again after 1 second. You has input wrong passwords for {T} time(s). '.replace('{T}', triedCount));
  94. return;
  95. };
  96. triedCount = 0;
  97. eval(code);
  98. };
  99.  
  100. // Run in Tempermonkey's MenuCommand is safe and needs no verification
  101. GM_registerMenuCommand('Execute javascript in Tempermonkey Sandbox', function() {
  102. const code = prompt('Your code:', 'GM_log(GM_Exec);');
  103. eval(code);
  104. });
  105. })();