Skip to content

Commit

Permalink
fix: 🐛 The affiliate tag URL injection broke some Amazon functionality.
Browse files Browse the repository at this point in the history
It uses now the correct Amazon affiliate URL structure on only relevant Amazon pages.
  • Loading branch information
simwai committed Dec 5, 2023
1 parent 5fd6d7d commit f3d2cea
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
42 changes: 27 additions & 15 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ async function handleNavigation(details) {
console.log('Handling navigation');

const websites = websitesData.map(x => x.hostSuffix);
const currentWebsite = new URL(details.url).hostname.replace(/^(www\.)/, '').replace(/^\./, ''); ;
const currentWebsite = new URL(details.url).hostname.replace(/^(www\.)/, '').replace(/^\./, '');

if (currentWebsite && websites.includes(currentWebsite) && !currentWebsite.includes('/ap/signin')) {
console.log('Generating affiliate link URL');
console.log('Generating affiliate URL...');
const websiteKey = currentWebsite.includes('amazon') ? 'amazon' : (currentWebsite.includes('aliexpress') ? 'aliexpress' : '');
let affiliateId = null;

try {
affiliateId = await getAffiliateIdForWebsite(websiteKey);
console.log('Found affiliate ID in LocalStorage: ' + affiliateId);
} catch (error) {
return console.error('Failed to get chrome storage. Refresh page.\n', error);
}
Expand All @@ -71,20 +72,25 @@ async function handleNavigation(details) {
}

function getAffiliateIdForWebsite(websiteKey) {
return new Promise(resolve => {
chrome.storage.sync.get(`${websiteKey}AffiliateId`, result => {
const affiliateId = result[`${websiteKey}AffiliateId`] || null;
resolve(affiliateId);
});
return new Promise((resolve, reject) => {
try {
chrome.storage.sync.get(`${websiteKey}AffiliateId`, result => {
const affiliateId = result[`${websiteKey}AffiliateId`] || null;
resolve(affiliateId);
});
} catch (error) {
reject(error);
}
});
}

function generateAffiliateLink(url, affiliateId, websiteKey) {
let modifiedURL = removeAffiliateParameters(url);
let modifiedURL = url;

if (websiteKey === 'amazon') {
modifiedURL = insertAffiliateId(modifiedURL, affiliateId);
modifiedURL = generateAmazonAffiliateLink(modifiedURL, affiliateId);
} else if (websiteKey === 'aliexpress') {
removeAffiliateParameters(url);
modifiedURL = generateAliExpressAffiliateLink(modifiedURL, affiliateId);
}

Expand All @@ -95,19 +101,25 @@ function removeAffiliateParameters(url) {
const baseURL = new URL(url);
const parameters = new URLSearchParams(baseURL.search);

parameters.delete('tag');
parameters.delete('aff_id');

baseURL.search = parameters.toString();
return baseURL.href;
}

function insertAffiliateId(url, affiliateId) {
function generateAmazonAffiliateLink(url, affiliateId) {
const baseURL = new URL(url);
const parameters = new URLSearchParams(baseURL.search);
parameters.set('tag', affiliateId);
baseURL.search = parameters.toString();
return baseURL.href;
const dpMatch = baseURL.pathname.match(/(.*)\/dp\/([A-Z0-9]{10})/g);
if (dpMatch?.[0]) {
// Rebuild the URL with the '/dp/[ASIN]' pattern
baseURL.pathname = dpMatch[0] + '/ref=nosim';
const parameters = new URLSearchParams();
parameters.set('tag', affiliateId);
baseURL.search = parameters.toString();
return baseURL.href;
} else {
return url;
}
}

function generateAliExpressAffiliateLink(url, affiliateId) {
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "Referral Link Inserter",
"version": "1.3",
"version": "1.4",
"description": "Inserts referral links into supported websites.",
"permissions": [
"storage",
Expand Down

0 comments on commit f3d2cea

Please sign in to comment.