-
Notifications
You must be signed in to change notification settings - Fork 0
/
batches.js
229 lines (205 loc) · 5.63 KB
/
batches.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
var batchCounter = -1;
function saveTabs(lastId) {
getCurrentURLs(function(urls) {
var name = document.getElementById("name").value;
name = name.substring(0, 20);
var batch = {
"Name":name,
"URLs":urls
};
insertBatch(batch, lastId);
});
}
function getCurrentURLs(callback) {
chrome.windows.getCurrent(function(result) {
var windowId = result["id"];
chrome.tabs.query({"windowId":windowId}, function(result) {
var urls = [];
var index = 0;
var editURL = "chrome-extension://" + chrome.runtime.id + "/edit.html";
for(var tab in result) {
var url = result[tab]["url"];
if(url != editURL) {
urls[index] = result[tab]["url"];
index++;
}
}
callback(urls);
});
});
}
function getBatches(callback) {
chrome.storage.sync.get("Batches", function(result) {
if(!result["Batches"] || result["Batches"] == null) {
var batches = JSON.parse('[{"Name": "Shopping","URLs": ["https://www.google.com/express/","http://www.amazon.com/","http://www.zappos.com/"]},{"Name": "Social","URLs": ["https://www.tumblr.com/","https://twitter.com/","https://www.facebook.com/"]},{"Name": "News","URLs": ["http://www.usatoday.com/","http://bleacherreport.com/","http://www.nytimes.com/"]}]');
}
else {
var batches = result["Batches"];
}
callback(batches);
});
}
function saveBatches(batches) {
chrome.storage.sync.set({"Batches": batches});
loadBatches();
}
function insertBatch(batch, lastId) {
getBatches(function(batches) {
var added = false;
for(var i in batches) {
if(!added && batch["Name"] == batches[i]["Name"]) {
batches[i] = batch;
added = true;
}
}
if(!added) {
if(lastId) {
batches.splice(lastId, 0, batch);
}
else {
batches[batches.length] = batch;
}
}
saveBatches(batches);
});
}
function loadBatches() {
getBatches(function(batches) {
document.getElementById("batches").innerHTML = "";
for(var i in batches) {
loadBatch(batches[i]);
}
});
}
function loadBatch(batch) {
var start = "batch_";
var end = batch["Name"].replace(/ /g, "_");
var openID = start + "open_" + end;
var editID = start + "edit_" + end;
var removeID = start + "remove_" + end;
var result = "<div id=\"batch_container_" + end + "\" class=\"batch\">";
result += "<a href=\"#\" class=\"batchText\" id=\"batch_text_" + end + "\">" + batch["Name"] + "</a>";
result += "<div class=\"batchActions\">";
result += "<a href=\"#\" class=\"batchAction\" id=\"" + openID + "\">New Window</a>";
result += "<a href=\"#\" class=\"batchAction\" id=\"" + editID + "\">Edit</a>";
result += "<a href=\"#\" class=\"batchAction\" id=\"" + removeID + "\">Remove</a>";
result += "</div>";
result += "</div>";
document.getElementById("batches").innerHTML += result;
}
function getBatchIndex(name, batches) {
for(var i in batches) {
if(name == batches[i]["Name"]) {
return i;
}
}
return -1;
}
function openBatch(name, newWindow) {
getBatches(function(batches) {
var i = getBatchIndex(name, batches);
if(i != -1) {
var batch = batches[i];
var urls = batch["URLs"];
affiliate(urls);
if(newWindow == true) {
openURLsInNewWindow(urls);
}
else {
openURLsInSameWindow(urls);
}
}
});
}
function openURLsInNewWindow(urls) {
var createData = {"url":urls};
chrome.windows.create(createData);
}
function openURLsInSameWindow(urls) {
var queryInfo = {"currentWindow":true};
chrome.tabs.query(queryInfo, function(tabs) {
for(var i in urls) {
var createProperties = {"url":urls[i]};
chrome.tabs.create(createProperties);
}
for(var i in tabs) {
var tab = tabs[i];
if(tab["url"] == "chrome://newtab/") {
chrome.tabs.remove(tab["id"]);
}
}
});
}
function affiliate(urls) {
if(!localStorage["OptOut"] || localStorage["OptOut"] == "false") {
for(var i in urls) {
var url = new URL(urls[i]);
if(url.hostname == "www.amazon.com") {
var urlString = url.toString().replace(url.search, "");
urlString += "?tag=batc03-20";
urls[i] = urlString;
}
}
}
}
function editBatch(name) {
localStorage["nextName"] = name;
getBatches(function(batches) {
var id = getBatchIndex(name, batches);
if(id != -1) {
localStorage["lastID"] = id;
var urls = batches[id]["URLs"];
var editURL = "chrome-extension://" + chrome.runtime.id + "/edit.html";
urls.splice(0, 0, editURL);
openURLsInNewWindow(urls);
}
});
}
function removeBatch(name) {
getBatches(function(batches) {
var i = getBatchIndex(name, batches);
if(i != -1) {
batches.splice(i, 1);
saveBatches(batches);
}
});
}
function message(text) {
document.getElementById("message").innerHTML = text;
}
function keyPressed(keyID) {
var batches = document.getElementById("batches").childNodes;
console.log(keyID);
if(batches.length > 0) {
if(keyID == "Up" && batchCounter >= 0) {
batchCounter--;
}
else if(keyID == "Down" && batchCounter < batches.length - 1) {
batchCounter++;
}
else if(keyID == "Enter" && batchCounter != -1) {
fireEvent(batches[batchCounter], "click");
}
if(batchCounter != -1) {
batches[batchCounter].className = "batch batch-hover";
if(batchCounter > 0) {
batches[batchCounter - 1].className = "batch";
}
if(batchCounter < batches.length - 1) {
batches[batchCounter + 1].className = "batch";
}
}
}
}
function fireEvent(element,event) {
if (document.createEvent) {
// dispatch for firefox + others
var evt = document.createEvent("HTMLEvents");
evt.initEvent(event, true, true ); // event type,bubbling,cancelable
return !element.dispatchEvent(evt);
} else {
// dispatch for IE
var evt = document.createEventObject();
return element.fireEvent('on'+event,evt);
}
}