// ==UserScript== // @name Fix AliExpress spam unsubscribe // @description AliExpress has unsubscribe links in their emails but some them are broken because AliExpress doesn't handle this email spam type. This userscript fixes it by adding support for any email spam type. // @namespace https://github.com/devnoname120 // @author devnoname120 // @version 0.1 // @match *://*.aliexpress.com/p/edm-setting/form.html* // @downloadURL https://gist.github.com/devnoname120/7fd8bebc1384dffc397fbe573a1fd2c7/raw/fix-aliexpress-unsubscribe.user.js // @grant none // @run-at document-start // ==/UserScript== // !!!IMPORTANT NOTE!!! You need to enable instant inject mode in the TamperMonkey settings or this script won't work. Here is how to do that: // // Settings --> General --> Config mode --> Set to "Advanced" // Settings --> Experimental --> Inject Mode --> Set to "Instant" (function() { 'use strict'; function patchScript(scriptText) { const urlParams = new URLSearchParams(window.location.search); const sub_code = urlParams.get('sub_code'); let modifiedScript = scriptText; // Fix the non-existing string for the subscription code by just using the strings for the first subscription code // The actual content of the strings is only displayed to the user (not used for logic) and it's something like "unsubcribe" // We extract the two variable names (in the example: j and w) to pattern match the replacements correctly. It looks like that: // {icon:"https://ae01.alicdn.com/kf/Se193868e904542c5947e4f3ea11323a2d/76x74.png",title:j[w][1]} const [, arr_name, index_name] = modifiedScript.match(/\btitle:(\w+)\[(\w+)\]\[/); // Apply the changes, for example: // j[w][ --> j[Object.keys(j)[0]][ modifiedScript = modifiedScript.replaceAll(`${arr_name}[${index_name}][`, `${arr_name}[Object.keys(${arr_name})[0]][`); // We extract the two variable names (in the example: j and w) to pattern match the replacements correctly. It looks like that: // "You've subscribed",desc:yr[br].resub}) const [, arr2_name, index2_name] = modifiedScript.match(/\bdesc:(\w+)\[(\w+)\]\.resub/); // Apply the changes, for example: // yr[br]. --> j[Object.keys(j)[0]][ modifiedScript = modifiedScript.replaceAll(`${arr2_name}[${index2_name}].`, `${arr2_name}[Object.keys(${arr2_name})[0]].`); // Inject the missing code into the list of codes to unsubscribe, for example: // codes:JSON.stringify(b) --> codes:JSON.stringify([...new Set(b.concat(Array.of(1004)))]) const regex2 = /codes:JSON\.stringify\((\w+)\)/g; modifiedScript = modifiedScript.replaceAll(regex2, (match, var_name) => { return `codes:JSON.stringify([...new Set(${var_name}.concat(Array.of(${sub_code})))])`; }); return modifiedScript; } // Function to intercept and modify the script response function interceptAndModifyScript(url) { fetch(url) .then(response => response.text()) .then(scriptText => { // Create a new script element and inject the modified script const scriptElement = document.createElement('script'); scriptElement.textContent = patchScript(scriptText); document.head.appendChild(scriptElement); }) .catch(error => console.error('Error fetching the script:', error)); } // Observe the DOM for the script element const observer = new MutationObserver(mutations => { for (let mutation of mutations) { if (mutation.type === 'childList') { for (let node of mutation.addedNodes) { if (node.tagName === 'SCRIPT') { // For example: assets.alicdn.com/g/ae-dida/edm-setting/0.0.10/form.js const scriptSrcPattern = /assets\.alicdn\.com\/.*edm-setting.*form\.js/; if (scriptSrcPattern.test(node.src)) { console.log(`Found "${node.src}", patching it...`); // Prevent the original script from loading node.remove(); // Intercept and modify the script interceptAndModifyScript(node.src); } } } } } }); console.log('Fix AliExpress spam unsubscribe started, searching for script to patch...'); // This code runs right after the DOM is created but before any of the page DOM is loaded. // We use an observer so that when the HTML loads we intercept the we create on the page const head = document.head || document.documentElement; if (head) { observer.observe(head, { childList: true, subtree: true }); } else { console.error("Fix AliExpress spam unsubscribe couldn't intercept the form.js script load. Make sure you properly enabled instant inject mode in the TamperMonkey settings."); } })();