forked from JupiterBroadcasting/JBAR-Chrome
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
105 lines (96 loc) · 3.96 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// // source URL // resulting url
// amazonuk // http://www.amazon.co.uk/?tag=tekno09-20
// amazon // http://www.amazon.com/?tag=tekno-21
// amazon // http://www.amazon.de/?tag=tekno03-21
// amazonfr // http://www.amazon.fr/?tag=tekno0b-21
// NOTE: Once this is finalized, you might want to STRINK/ofuscate this some how.
var configurations = {
amazon : {
rx: /^http.*?\.amazon\.com.*?(\/dp\/|\/o\/asin\/|\/exec\/obidos\/tg\/detail\/|\/gp\/product\/)/i,
params: [
{ param: "tag", paramValue: "tekno09-20" }
]
},
amazonuk : {
rx: /^http.*?\.amazon\.co\.uk.*?(\/dp\/|\/o\/asin\/|\/exec\/obidos\/tg\/detail\/|\/gp\/product\/)/i,
params: [
{ param: "tag", paramValue: "tekno-21" }
]
},
amazonde : {
rx: /^http.*?\.amazon\.de.*?(\/dp\/|\/o\/asin\/|\/exec\/obidos\/tg\/detail\/|\/gp\/product\/)/i,
params: [
{ param: "tag", paramValue: "tekno03-21" }
]
},
amazonfr : {
rx: /^http.*?\.amazon\.fr.*?(\/dp\/|\/o\/asin\/|\/exec\/obidos\/tg\/detail\/|\/gp\/product\/)/i,
params: [
{ param: "tag", paramValue: "tekno0b-21" }
]
},
};
function addTag(info) {
var tUrl = info.url;
var r = { cancel: false };
console.log("Inside addTag() ");
for ( var config in configurations) {
if( configurations.hasOwnProperty(config) ) {
if (tUrl.match(configurations[config].rx) ) {
//gracefully acknowledge existing affiliate tags
if (tUrl.indexOf(configurations[config].params[0].param) == -1 ) {
r = { redirectUrl: tUrl+(tUrl.indexOf("?") == -1 ? "?" : "&") + createTag(configurations[config].params) };
// A supported site was found
// get the current window
chrome.windows.getCurrent(function (currentWindow) {
// get the selected tab inside the current window
chrome.tabs.query({active: true, windowId: currentWindow.id}, function(tabs) {
chrome.pageAction.show(tabs[0].id);
});
});
break;
}
}
}
}
return r;
}
function createTag(params) {
var result = "";
for( var i = 0; i < params.length; i++ ) {
result = result + params[i].param + "=" + params[i].paramValue;
if( i >= 0 && i < params.length - 1 ) {
result = result + "&";
}
}
return result;
}
if (!chrome.webRequest.onBeforeRequest.hasListener(addTag)) {
// var site_urls = [];
// for (x in sites) {
// site_urls.push("*://*."+sites[x].url+"/*");
// }
var site_urls = [
"http://*.amazon.com/*/dp/*",
"http://*.amazon.com/dp/*",
"http://*.amazon.com/exec/obidos/tg/detail/*",
"http://*.amazon.com/gp/product/*",
"http://*.amazon.com/o/*",
"http://*.amazon.co.uk/*/dp/*",
"http://*.amazon.co.uk/dp/*",
"http://*.amazon.co.uk/exec/obidos/tg/detail/*",
"http://*.amazon.co.uk/gp/product/*",
"http://*.amazon.co.uk/o/*",
"http://*.amazon.de/*/dp/*",
"http://*.amazon.de/dp/*",
"http://*.amazon.de/exec/obidos/tg/detail/*",
"http://*.amazon.de/gp/product/*",
"http://*.amazon.de/o/*",
"http://*.amazon.fr/*/dp/*",
"http://*.amazon.fr/dp/*",
"http://*.amazon.fr/exec/obidos/tg/detail/*",
"http://*.amazon.fr/gp/product/*",
"http://*.amazon.fr/o/*",
];
chrome.webRequest.onBeforeRequest.addListener(addTag, { urls: site_urls }, [ "blocking" ]);
}