AjaxPipeHelper

Page => AJAX Page

当前为 2023-11-15 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name AjaxPipeHelper
  3. // @author -
  4. // @namespace AjaxPipeHelper - Scripts
  5. // @description Page => AJAX Page
  6. // @license Creative Commons Attribution License
  7. // @version 0.1
  8. // @include *://old.reddit.com/*
  9. // @grant none
  10. // @run-at document-start
  11. // ==/UserScript==
  12.  
  13. function parseResponseHeaders(headerStr) {
  14. var headers = {};
  15. if (!headerStr) {
  16. return headers;
  17. }
  18. var headerPairs = headerStr.split('\u000d\u000a');
  19. for (var i = 0, len = headerPairs.length; i < len; i++) {
  20. var headerPair = headerPairs[i];
  21. var index = headerPair.indexOf('\u003a\u0020');
  22. if (index > 0) {
  23. var key = headerPair.substring(0, index);
  24. var val = headerPair.substring(index + 2);
  25. headers[key.toLowerCase()] = val;
  26. }
  27. }
  28. return headers;
  29. }
  30.  
  31. function ajaxpiperenabler() {
  32. document.addEventListener("click", onclickact);
  33. window.addEventListener("popstate", poppye);
  34.  
  35. function poppye() {
  36. ajaxpipefetcher(location.href)
  37. }
  38.  
  39. function ajaxpipefetcher(currenttag) {
  40. GM_xmlhttpRequest({
  41. method: "GET",
  42. url: currenttag,
  43. stillpagefilter: true,
  44. onload: function (response) {
  45. var responseheaders = parseResponseHeaders(response.responseHeaders)
  46. if (responseheaders['content-type'].indexOf('text/html') != -1) {
  47. document.body.innerHTML = response.responseText;
  48. } else {
  49. location = currenttag
  50. }
  51. scrollTo(0, 0)
  52. window.history.pushState(null, null, currenttag);
  53. }
  54. });
  55.  
  56. }
  57.  
  58. function onclickact(e) {
  59. //re = "(?:javascript|.(?:jpe?g|gif|png|js|css|json|exe|zip|rar|iso|7z|ahk))";
  60. var currenttag = GM_getParentByTagName(e.target, "a")
  61. if (e.button === 0) {
  62. if ((currenttag.nodeName == "A") && (currenttag.href.indexOf(location.hostname) != -1) && (!currenttag.getAttribute("onclick")) && (!currenttag.getAttribute("data-toggle"))) {
  63. e.preventDefault();
  64. ajaxpipefetcher(currenttag.href)
  65. }
  66. }
  67. }
  68. }
  69. ajaxpiperenabler()