Skip to content

Instantly share code, notes, and snippets.

@devnoname120
Last active August 8, 2024 12:27
Show Gist options
  • Save devnoname120/7fd8bebc1384dffc397fbe573a1fd2c7 to your computer and use it in GitHub Desktop.
Save devnoname120/7fd8bebc1384dffc397fbe573a1fd2c7 to your computer and use it in GitHub Desktop.
fix-aliexpress-unsubscribe.user.js

How to install on Google Chrome

  1. Install the Tampermonkey extension.
  2. Do a right-click on the Tampermonkey icon and press Options.
  3. Click on Settings, and then in the General section change the Config mode to Advanced.
  4. In the Experiments section at the bottom of the page, change the Inject Mode to Instant.
  5. You can close this settings page now.
  6. Click on this link and press on Install in the Tampermonkey tab that just opened.
  7. Voilà ! You should now be able to unsubscribe from the broken unsubscribe links.
// ==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 <script src="..."> element before it gets executed to get rid of it
// Then we do an XHR request to get the content of the remote script ourselves, we patch it, and then inject the patched JS code into a <script>...</script> 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.");
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment