XHReload

Reloading page with an XML HTTP Request type of it. Hotkey: Command + Shift + H.

当前为 2024-04-24 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name XHReload
  3. // @namespace i2p.schimon.xhreload
  4. // @description Reloading page with an XML HTTP Request type of it. Hotkey: Command + Shift + H.
  5. // @homepageURL https://greasyfork.org/scripts/493323-xhreload
  6. // @supportURL https://greasyfork.org/scripts/493323-xhreload/feedback
  7. // @copyright 2024, Schimon Jehudah (http://schimon.i2p)
  8. // @license MIT; https://opensource.org/licenses/MIT
  9. // @match *://*/*
  10. // @version 24.04
  11. // @run-at document-start
  12. // @grant GM.registerMenuCommand
  13. // @icon data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxMDAgMTAwIj48dGV4dCB5PSIuOWVtIiBmb250LXNpemU9IjkwIj7imbvvuI88L3RleHQ+PC9zdmc+Cg==
  14. // ==/UserScript==
  15.  
  16. function xhreload() {
  17. let request = new XMLHttpRequest();
  18. request.open('GET', document.documentURI);
  19. request.onload = function() {
  20. if (request.status >= 200 && request.status < 300) {
  21. let domParser = new DOMParser();
  22. //let htmlFile = domParser.parseFromString(request.response.trim(), 'text/html');
  23. let htmlFile = domParser.parseFromString(request.responseText, 'text/html');
  24. let newDocument = document.importNode(htmlFile.documentElement, true);
  25. let oldDocument = document.documentElement;
  26. document.replaceChild(newDocument, oldDocument);
  27. } else {
  28. let errorMessage = 'Request failed with status: ' + request.status;
  29. console.error(errorMessage);
  30. alert(errorMessage);
  31. }
  32. };
  33. request.send();
  34. }
  35.  
  36. function infoBar() {
  37. let namespace = 'i2p-schimon-ruffle';
  38. let bar = document.createElement(namespace);
  39. document.body.append(bar);
  40. bar.innerHTML = 'XHR Reload';
  41. bar.title = 'Flash elements have been detected on this page. Click this bar to activate Ruffle player.';
  42. bar.id = namespace;
  43. bar.style.backgroundColor = '#fff';
  44. bar.style.color = '#000';
  45. bar.style.fontFamily = 'system-ui';
  46. bar.style.fontSize = 'larger';
  47. bar.style.fontWeight = 'bold';
  48. bar.style.right = 0;
  49. bar.style.left = 0;
  50. bar.style.top = 0;
  51. bar.style.zIndex = 10000000000;
  52. bar.style.padding = '6px'; //13px //15px //11px //9px //3px //1px
  53. bar.style.position = 'fixed';
  54. bar.style.textAlign = 'center'; // justify
  55. bar.style.direction = 'ltr';
  56. bar.style.userSelect = 'none';
  57. // set bar behaviour
  58. bar.onclick = () => {
  59. bar.style.display = 'none';
  60. xhreload();
  61. };
  62. }
  63.  
  64. function loadAtPageLoad(){
  65. document.addEventListener("DOMContentLoaded",
  66. async function() {
  67. infoBar();
  68. }
  69. );
  70. }
  71.  
  72. function hotkey(e) {
  73. console.log(e)
  74. if (e.metaKey && e.shiftKey && e.which == 72) {
  75. xhreload();
  76. }
  77. }
  78.  
  79. (function() {
  80. document.addEventListener('keyup', hotkey, false);
  81. //document.documentURI
  82. //document.location.search
  83. //location.search
  84. if (location.search.includes('__cf_chl_rt_tk')) {
  85. loadAtPageLoad();
  86. }
  87. })();
  88.  
  89. (async function registerMenuCommand(){
  90. try {
  91. await GM.registerMenuCommand('Rough Reload Command + Shift + H', () => xhreload(), 'H');
  92. } catch (err) {
  93. console.warn(err);
  94. console.info('API GM.registerMenuCommand does not seem to be available.');
  95. }
  96. })();
  97.  
  98. (function() {
  99. document.addEventListener('DOMContentLoaded', function() {
  100. if (window.location.search.includes('__cf_chl_rt_tk')) {
  101. xhreload();
  102. }
  103. });
  104. })();