Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions manifest-v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@
"js": ["build/js/content_script.js"],
"run_at": "document_end"
},
{
"matches": [
"*://*.wegobuy.com/*",
"*://*.superbuy.com/*",
"*://*.sugargoo.com/*",
"*://*.cssbuy.com/*",
"*://*.pandabuy.com/*",
"*://*.hagobuy.com/*",
"*://*.kameymall.com/*",
"*://*.ezbuycn.com/*",
"*://*.cnfans.com/*",
"*://*.hoobuy.com/*"
],
"js": ["build/js/affiliates.js"],
"run_at": "document_end"
},
{
"matches": [
"*://*.wegobuy.com/*",
Expand Down
18 changes: 17 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@
"js": ["build/js/content_script.js"],
"run_at": "document_end"
},
{
"matches": [
"*://*.wegobuy.com/*",
"*://*.superbuy.com/*",
"*://*.sugargoo.com/*",
"*://*.cssbuy.com/*",
"*://*.pandabuy.com/*",
"*://*.hagobuy.com/*",
"*://*.kameymall.com/*",
"*://*.ezbuycn.com/*",
"*://*.cnfans.com/*",
"*://*.hoobuy.com/*"
],
"js": ["build/js/affiliates.js"],
"run_at": "document_end"
},
{
"matches": [
"*://*.wegobuy.com/*",
Expand Down Expand Up @@ -75,5 +91,5 @@
"*://*.cnfans.com/*",
"*://*.hoobuy.com/*"
],
"permissions": ["storage", "webNavigation", "scripting"]
"permissions": ["storage", "webNavigation", "scripting", "cookies"]
}
20 changes: 20 additions & 0 deletions src/affiliates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { getAffiliates } from './lib/getAffiliates';
import { loadSettings } from './lib/loadSettings';
import { setAffiliateLocalStorage } from './lib/setAffiliateLocalStorage';
import { settingNames } from './models/Settings';

async function main() {
const settings = await loadSettings(settingNames);
if (!settings?.affiliateProgram) return false;
const affiliateLinks = await getAffiliates();
if (!affiliateLinks) return null;

const currentUrl = new URL(window.location.href);

setAffiliateLocalStorage(affiliateLinks, currentUrl);
return true;
}

main();

export {};
44 changes: 8 additions & 36 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Config } from './Config';
import { redirectListenerUrls } from './data/redirectListenerUrls';
import { fetchData } from './lib/api/fetchData';
import { getAffiliates } from './lib/getAffiliates';
import { setAffiliateCookies } from './lib/setAffiliateCookies';
import { getStorage, isChromeStorage } from './lib/storage';
import type { Settings } from './models/Settings';
import { defaultSettings } from './models/Settings';
Expand Down Expand Up @@ -80,44 +81,15 @@ function initializeExtension(
}
}

/**
* Adds a listener to listen for redirect events.
*/
function addRedirectListener(isChrome: boolean) {
if (isChrome) {
chrome.webNavigation.onHistoryStateUpdated.addListener(
(details) => {
chrome.scripting.executeScript({
target: { tabId: details.tabId, allFrames: true },
files: ['build/js/redirect.js'],
});
},
{
url: redirectListenerUrls,
}
);
} else {
browser.webNavigation.onHistoryStateUpdated.addListener((details) => {
console.error(JSON.stringify(details));
browser.tabs
.executeScript(details.tabId, {
file: './js/redirect.js',
allFrames: true,
})
.then((result) => {
console.log(result);
})
.catch((error) => {
console.error('Error executing script:', error);
});
});
}
}

function main() {
const storage = getStorage();
initializeExtension(storage);
addRedirectListener(isChromeStorage(storage));
// addRedirectListener(isChromeStorage(storage));
getAffiliates().then((affiliates) => {
if (affiliates) {
setAffiliateCookies(isChromeStorage(storage), affiliates);
}
});
}

main();
106 changes: 106 additions & 0 deletions src/lib/setAffiliateCookies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { type Agent, agents } from 'cn-links';

import { Config } from '../Config';
import type { AffiliateLink, AffiliateLinks } from '../models';

export function getCookie(
isChrome: boolean,
agent: Agent,
affiliate: AffiliateLink
): null | browser.cookies._SetDetails[] | chrome.cookies.SetDetails[] {
const shared = {
value: affiliate.signupRef,
path: '/',
expirationDate: 1893456000,
};
if (agent === 'hoobuy') {
return [
{
...shared,
url: 'https://hoobuy.com',
domain: 'hoobuy.com',
name: 'inviteCode',
},
];
}
if (agent === 'sugargoo') {
return null;
}
if (agent === 'wegobuy') {
return [
{
...shared,
url: 'https://www.wegobuy.com',
domain: '.wegobuy.com',
name: 'wegobuyLoginInviteCode',
},
];
}
if (agent === 'superbuy') {
return [
{
...shared,
url: 'https://www.superbuy.com',
domain: '.superbuy.com',
name: 'superbuyLoginInviteCode',
},
];
}
if (agent === 'cssbuy') {
if (!affiliate.cookie) {
console.error('Agent cssbuy has no cookie key');
return null;
}
const obj = JSON.parse(affiliate.cookie);
return [
{
...shared,
url: 'https://www.cssbuy.com',
domain: 'www.cssbuy.com',
name: Object.keys(obj)[0],
value: Object.values(obj)[0] as string,
},
{
...shared,
url: 'https://www.cssbuy.com',
domain: '.cssbuy.com',
name: 'refererurl',
value: encodeURIComponent(`${Config.host.details}/tools/extension`),
},
];
}
if (agent === 'hagobuy') {
return [
{
...shared,
url: 'https://www.hagobuy.com',
domain: '.hagobuy.com',
name: 'AFFCODE',
},
];
}
if (agent === 'pandabuy') {
return null;
}
return null;
}

export async function setAffiliateCookies(
isChrome: boolean,
affiliateLinks: AffiliateLinks
) {
agents.forEach((agent) => {
const values = getCookie(isChrome, agent, affiliateLinks[agent]);
if (values && values.length > 0) {
if (isChrome) {
values.map((value) => chrome.cookies?.set(value));
} else {
values.map((value) =>
browser.cookies?.set({ ...value, sameSite: 'no_restriction' })
);
}
}
});

return null;
}
59 changes: 59 additions & 0 deletions src/lib/setAffiliateLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type { Agent } from 'cn-links';
import { detectAgent } from 'cn-links';

import type { AffiliateLink, AffiliateLinks } from '../models';

type LocalStorageKV = { [x in string]: string };

class LocalStorage {
static set(obj: LocalStorageKV) {
window.localStorage.setItem(Object.keys(obj)[0], Object.values(obj)[0]);
}
}

export function setCookie(
agent: Agent,
affiliate: AffiliateLink
): null | boolean {
if (agent === 'hoobuy') {
return true;
}
if (agent === 'sugargoo') {
if (!affiliate.localStorage) {
console.error('Agent sugargoo has no localStorage key');
return false;
}
LocalStorage.set(JSON.parse(affiliate.localStorage));
} else if (agent === 'pandabuy') {
const value = {
inviteCode: affiliate.signupRef,
setTime: new Date().getTime(),
};
LocalStorage.set({ INVITECODE: JSON.stringify(value) });
} else if (agent === 'superbuy') {
return null;
} else if (agent === 'wegobuy') {
return null;
} else if (agent === 'hagobuy') {
return null;
} else if (agent === 'cssbuy') {
return null;
} else {
console.log(`Agent ${agent} has no known affiliate key`);
}

return null;
}

export async function setAffiliateLocalStorage(
affiliateLinks: AffiliateLinks,
currentUrl: URL
) {
const agent = detectAgent(currentUrl);
if (!agent) {
console.error('No agent detected');
return null;
}
setCookie(agent, affiliateLinks[agent]);
return null;
}
2 changes: 2 additions & 0 deletions src/models/Affiliate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export type AffiliateLink = {
itemRef: string;
incentive?: string;
signupLink?: string;
localStorage?: string;
cookie?: string;
};

export type AffiliateLinks = {
Expand Down
55 changes: 0 additions & 55 deletions src/redirect.ts

This file was deleted.

2 changes: 1 addition & 1 deletion webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ module.exports = {
index: './src/index.tsx',
background: './src/background.ts',
content_script: './src/content_script.ts',
redirect: './src/redirect.ts',
toolbar: './src/toolbar.ts',
affiliates: './src/affiliates.ts',
},
mode: 'production',
module: {
Expand Down