Skip to content

Instantly share code, notes, and snippets.

@ssut
Last active May 20, 2021 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssut/b25fd34087d355740ab415ccf2f925e8 to your computer and use it in GitHub Desktop.
Save ssut/b25fd34087d355740ab415ccf2f925e8 to your computer and use it in GitHub Desktop.
NoReferrer
// ==UserScript==
// @name NoReferrer
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match http*://*/*
// @grant GM_xmlhttpRequest
// @connect coupa.ng
// @connect a.aliexpress.com
// @connect s.click.aliexpress.com
// @connect star.aliexpress.com
// @connect *.aliexpress.com
// @require https://greasyfork.org/scripts/421384-gm-fetch/code/GM_fetch.js?version=898562
// @icon https://www.google.com/s2/favicons?domain=linkprice.com
// ==/UserScript==
(function() {
'use strict';
const style = document.createElement('style');
style.type = 'text/css';
const modifiedClassName = `link-${String(Math.random()).replace(/[^\d]/g, '')}`;
const styles = `.${modifiedClassName} {
text-decoration: underline !important;
text-decoration-thickness: 0.4em !important;
text-decoration-skip-ink: none !important;
text-decoration-color: #ffe0005e !important;
transition: text-decoration-color 0.2s !important;
}
.${modifiedClassName}:hover {
text-decoration-color: #ffe000ab !important;
}
`;
if (style.styleSheet) {
style.styleSheet.cssText = styles;
} else {
style.appendChild(document.createTextNode(styles));
}
(document.querySelector('head') ?? document.body).appendChild(style);
const targetDomains = [
'click.linkprice.com',
's.ppomppu.co.kr',
'link.coupang.com',
'www.amazon.com',
'click.aliexpress.com',
];
const specialDomains = [
'coupa.ng',
'a.aliexpress.com',
's.click.aliexpress.com',
];
const handleSpecialDomain = async (domain, link) => {
link.rel = 'noreferrer noopener';
const response = await GM_fetch(link.href);
switch (domain) {
case 'coupa.ng': {
const finalUrl = new URL(response.finalUrl);
for (const key of [...finalUrl.searchParams.keys()]) {
if (!['itemId', 'vendorItemId'].includes(key)) {
finalUrl.searchParams.delete(key);
}
}
link.href = finalUrl.toString();
link.classList.add(modifiedClassName);
} break;
case 'a.aliexpress.com':
case 's.click.aliexpress.com': {
const intermediateUrl = new URL(response.finalUrl);
const redirectUrl = new URL(intermediateUrl.searchParams.get('redirectUrl'));
redirectUrl.search = '';
link.href = redirectUrl.toString();
link.classList.add(modifiedClassName);
} break;
}
};
const links = document.getElementsByTagName('a');
console.debug('[NoReferrer]', 'links', links);
for (const link of links) {
const specialCase = specialDomains.find((targetDomain) => link.href.includes(targetDomain));
if (specialCase) {
handleSpecialDomain(specialCase, link).catch(console.error);
continue;
} else if (targetDomains.findIndex((targetDomain) => link.href.includes(targetDomain)) === -1) {
continue;
}
const hasDifferentLink = link.textContent.match(/^(\s+)?https?\:\/\//) && link.textContent !== link.href;
console.debug('[NoReferrer]', 'hasDifferentLink?', hasDifferentLink, { href: link.href, text: link.textContent.trim() });
if (hasDifferentLink) {
let href = link.textContent.trim();
try {
const url = new URL(href);
if (url.searchParams.has('ref')) {
url.searchParams.delete('ref');
}
href = url.toString();
} catch {}
link.href = href;
link.classList.add(modifiedClassName);
}
link.rel = 'noreferrer noopener';
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment