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.

Revisions

  1. devnoname120 revised this gist Aug 8, 2024. 2 changed files with 12 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions aREADME.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    # How to install on Google Chrome

    1. Install the [Tampermonkey extension](https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo).
    2. Do a right-click on the Tampermonkey icon and press `Options`.
    4. Click on `Settings`, and then in the `General` section change the `Config mode` to `Advanced`.
    <img src="https://gist.github.com/assets/2824100/c0998f65-9dc4-43bb-a671-147f8bd009c4" width="600"><br>
    6. In the `Experiments` section at the bottom of the page, change the `Inject Mode` to `Instant`.
    <img src="https://gist.github.com/assets/2824100/4d2570a8-2c84-4926-bd57-b3fd0a08a5a5" width="500"><br>
    7. You can close this settings page now.
    8. Click on [this link](https://gist.github.com/devnoname120/7fd8bebc1384dffc397fbe573a1fd2c7/raw/fix-aliexpress-unsubscribe.user.js) and press on `Install` in the Tampermonkey tab that just opened.
    9. Voilà ! You should now be able to unsubscribe from the broken unsubscribe links.
    1 change: 1 addition & 0 deletions fix-aliexpress-unsubscribe.user.js
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,7 @@
    // @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==
  2. devnoname120 renamed this gist Aug 8, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. devnoname120 created this gist Aug 8, 2024.
    112 changes: 112 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,112 @@
    // ==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*
    // @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.");
    }
    })();