// ==UserScript==
// @name Replace webp with format in image URLs on WeChat
// @namespace http://your.namespace.com
// @version 0.1
// @description Replace 'tp=webp' with format in image URLs on WeChat
// @author Your Name
// @match https://mp.weixin.qq.com/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
function replaceWebpWithFormat() {
var images = document.querySelectorAll('img');
images.forEach(function(img) {
var src = img.getAttribute('src');
if (src && src.includes('tp=webp')) {
var format = getFormat(src);
if (format) {
src = src.replace('tp=webp', 'tp=' + format);
img.setAttribute('src', src);
img.setAttribute('data-src', src); // If using lazy loading
}
}
});
}
function getFormat(url) {
var match = url.match(/wx_fmt=([^&]+)/);
if (match && match[1]) {
return match[1];
}
return null;
}
// Run after page load
window.addEventListener('load', function() {
replaceWebpWithFormat();
// Run again if new content is loaded dynamically
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
replaceWebpWithFormat();
}
});
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
})();