Duanmao HTTPS

使断锚网页版支持 HTTPS

目前为 2021-08-28 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Duanmao HTTPS
  3. // @namespace CCCC_David
  4. // @version 0.2.1
  5. // @description 使断锚网页版支持 HTTPS
  6. // @author CCCC_David
  7. // @match https://duanmao.top/*
  8. // @run-at document-start
  9. // @grant none
  10. // @require https://unpkg.com/ajax-hook/dist/ajaxhook.min.js
  11. // ==/UserScript==
  12.  
  13. (() => {
  14. 'use strict';
  15.  
  16. const fixURL = (url) => url === undefined ? undefined : url.replace(/^https?:\/\/cdn1\.duanmao\.top\//i, 'https://duanmao.top/')
  17. .replace(/^http:\/\//i, 'https://');
  18. ah.proxy({
  19. onRequest: (config, handler) => {
  20. config.url = fixURL(config.url);
  21. handler.next(config);
  22. },
  23. onError: (err, handler) => {
  24. handler.next(err);
  25. },
  26. onResponse: (response, handler) => {
  27. handler.next(response);
  28. },
  29. });
  30.  
  31. const isForumPage = window.location.pathname === '/forum';
  32.  
  33. const addMetaTags = () => {
  34. if (isForumPage) {
  35. return;
  36. }
  37. const metaReferrer = document.createElement('meta');
  38. metaReferrer.name = 'referrer';
  39. metaReferrer.content = 'same-origin';
  40. document.head.appendChild(metaReferrer);
  41. const metaCSP = document.createElement('meta');
  42. metaCSP.httpEquiv = 'Content-Security-Policy';
  43. metaCSP.content = "base-uri 'self'; script-src 'self'; object-src 'none'; upgrade-insecure-requests";
  44. document.head.appendChild(metaCSP);
  45. };
  46.  
  47. const updateElementURL = (el) => {
  48. const tagName = el.nodeName.toLowerCase();
  49. // There are many HTML tag attributes which can include URLs
  50. // (see https://stackoverflow.com/questions/2725156/complete-list-of-html-tag-attributes-which-have-a-url-value),
  51. // but here we just deal with the most common two cases:
  52. // - <a href="..."></a>
  53. // - <img src="..." />
  54. if (tagName === 'a') {
  55. el.href = fixURL(el.href);
  56. } else if (tagName === 'img') {
  57. el.src = fixURL(el.src);
  58. }
  59. };
  60.  
  61. const observer = new MutationObserver((mutationsList) => {
  62. for (const mutation of mutationsList) {
  63. if (mutation.type === 'childList') {
  64. for (const el of mutation.addedNodes) {
  65. if (el.nodeName.toLowerCase() === 'head') {
  66. addMetaTags();
  67. }
  68. updateElementURL(el);
  69. }
  70. }
  71. }
  72. });
  73.  
  74. observer.observe(document.documentElement, {
  75. subtree: true,
  76. childList: true,
  77. });
  78.  
  79. if (document.head) {
  80. addMetaTags();
  81. }
  82.  
  83. for (const tagName of ['a', 'img']) {
  84. for (const el of document.getElementsByTagName(tagName)) {
  85. updateElementURL(el);
  86. }
  87. }
  88.  
  89. const insertScriptInline = (script) => {
  90. const el = document.createElement('script');
  91. el.appendChild(document.createTextNode(script));
  92. document.body.appendChild(el);
  93. };
  94.  
  95. const insertScriptSrc = (src) => {
  96. const el = document.createElement('script');
  97. el.src = src;
  98. document.body.appendChild(el);
  99. };
  100.  
  101. if (isForumPage) {
  102. document.documentElement.innerHTML = `
  103. <!DOCTYPE html>
  104. <html lang="zh-CN">
  105. <head>
  106. <meta charset="utf-8">
  107. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  108. <meta name="referrer" content="same-origin">
  109. <meta http-equiv="Content-Security-Policy" content="base-uri 'self'; script-src 'self' 'sha256-tjTO3rQxUkgLj//lUhUlsDm7/uSQn+aGhV+Lb2IwohA='; object-src 'none'; upgrade-insecure-requests">
  110. <title>断锚</title>
  111. <link rel="stylesheet" href="/static/index.d0f128df.css">
  112. </head>
  113. <body><noscript><strong>Please enable JavaScript to continue.</strong></noscript>
  114. <div id="app"></div>
  115. </body>
  116. </html>
  117. `;
  118. insertScriptInline(`
  119. (() => {
  120. window.coverSupport = 'CSS' in window && typeof CSS.supports === 'function' && (CSS.supports('top: env(a)') || CSS.supports('top: constant(a)'));
  121. const metaViewport = document.createElement('meta');
  122. metaViewport.name = 'viewport';
  123. metaViewport.content = 'width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0' + (window.coverSupport ? ', viewport-fit=cover' : '');
  124. document.head.appendChild(metaViewport);
  125. })();
  126. `);
  127. insertScriptSrc('/static/js/chunk-vendors.6e103c75.js');
  128. insertScriptSrc('/static/js/index.37f94612.js');
  129. }
  130.  
  131. console.log('Duanmao HTTPS script is loaded');
  132. })();