// ==UserScript==
// @name Duanmao HTTPS
// @namespace CCCC_David
// @version 0.2.0
// @description 使断锚网页版支持 HTTPS
// @author CCCC_David
// @match https://duanmao.top/*
// @run-at document-start
// @grant none
// @require https://unpkg.com/ajax-hook/dist/ajaxhook.min.js
// @downloadURL none
// ==/UserScript==
(() => {
'use strict';
const fixURL = (url) => url === undefined ? undefined : url.replace(/^https?:\/\/cdn1\.duanmao\.top\//i, 'https://duanmao.top/')
.replace(/^http:\/\//i, 'https://');
ah.proxy({
onRequest: (config, handler) => {
config.url = fixURL(config.url);
handler.next(config);
},
onError: (err, handler) => {
handler.next(err);
},
onResponse: (response, handler) => {
handler.next(response);
},
});
const isForumPage = window.location.pathname === '/forum';
const addMetaTags = () => {
if (isForumPage) {
return;
}
const metaReferrer = document.createElement('meta');
metaReferrer.name = 'referrer';
metaReferrer.content = 'same-origin';
document.head.appendChild(metaReferrer);
const metaCSP = document.createElement('meta');
metaCSP.httpEquiv = 'Content-Security-Policy';
metaCSP.content = "base-uri 'self'; script-src 'self'; object-src 'none'; upgrade-insecure-requests";
document.head.appendChild(metaCSP);
};
const updateElementURL = (el) => {
const tagName = el.nodeName.toLowerCase();
// There are many HTML tag attributes which can include URLs
// (see https://stackoverflow.com/questions/2725156/complete-list-of-html-tag-attributes-which-have-a-url-value),
// but here we just deal with the most common two cases:
// -
// -
if (tagName === 'a') {
el.href = fixURL(el.href);
} else if (tagName === 'img') {
el.src = fixURL(el.src);
}
};
const observer = new MutationObserver((mutationsList) => {
for (const mutation of mutationsList) {
if (mutation.type === 'childList') {
for (const el of mutation.addedNodes) {
if (el.nodeName.toLowerCase() === 'head') {
addMetaTags();
}
updateElementURL(el);
}
}
}
});
observer.observe(document.documentElement, {
subtree: true,
childList: true,
});
if (document.head) {
addMetaTags();
}
for (const tagName of ['a', 'img']) {
for (const el of document.getElementsByTagName(tagName)) {
updateElementURL(el);
}
}
const insertScriptInline = (script) => {
const el = document.createElement('script');
el.appendChild(document.createTextNode(script));
document.body.appendChild(el);
};
const insertScriptSrc = (src) => {
const el = document.createElement('script');
el.src = src;
document.body.appendChild(el);
};
if (isForumPage) {
document.documentElement.innerHTML = `