Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ImoutoHeaven/fab81a9624ddc7f8b4eaa0efc8e0797c to your computer and use it in GitHub Desktop.

Select an option

Save ImoutoHeaven/fab81a9624ddc7f8b4eaa0efc8e0797c to your computer and use it in GitHub Desktop.
rewrite-shenduba.user.js
// ==UserScript==
// @name Restore Original Image URLs
// @namespace restore-original-image-urls
// @version 0.1
// @description 将部分图片代理地址还原为原始图片地址,避免另存为 webp
// @match *://*/*
// @run-at document-start
// ==/UserScript==
(function () {
'use strict';
function restoreUrl(url) {
if (!url) return url;
return url
.replace(/^https:\/\/i\d\.wp\.com\/raw\.githubusercontent\.com\//, 'https://raw.githubusercontent.com/')
.replace(/^https:\/\/i\d\.wp\.com\//, 'https://');
}
function processElement(el) {
const attrs = ['src', 'href', 'data-src', 'data-original', 'data-lazy-src'];
for (const attr of attrs) {
const value = el.getAttribute?.(attr);
if (!value) continue;
const restored = restoreUrl(value);
if (restored !== value) {
el.setAttribute(attr, restored);
}
}
const srcset = el.getAttribute?.('srcset');
if (srcset) {
const restored = srcset.replace(/https:\/\/i\d\.wp\.com\/raw\.githubusercontent\.com\//g, 'https://raw.githubusercontent.com/');
if (restored !== srcset) {
el.setAttribute('srcset', restored);
}
}
}
function processPage() {
document.querySelectorAll('img, a, source').forEach(processElement);
}
const observer = new MutationObserver(() => processPage());
function start() {
processPage();
observer.observe(document.documentElement, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ['src', 'href', 'srcset', 'data-src', 'data-original', 'data-lazy-src'],
});
}
if (document.documentElement) {
start();
} else {
document.addEventListener('DOMContentLoaded', start, { once: true });
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment