-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
206 lines (169 loc) · 5.67 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
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
"use strict";
const OPTION_DEFAULTS = {
linkedImagesRegexp: "\\.(jpg|jpeg|png|gifv)$",
minWidth: 200,
minHeight: 300,
};
let currentTab = undefined;
browser.tabs.query({active: true, currentWindow: true}).then((tabs) => { currentTab = tabs[0].id; });
browser.tabs.onActivated.addListener((activeInfo) => { currentTab = activeInfo.tabId; });
browser.windows.onFocusChanged.addListener((windowId) => {
browser.tabs.query({active: true, windowId: windowId}).then((tabs) => { currentTab = tabs[0].id; });
});
const state = {};
browser.storage.local.get(null)
.then((result) => {
if (Object.keys(result).length == 0) {
browser.storage.local.set(OPTION_DEFAULTS);
}
});
browser.commands.onCommand.addListener(function(command) {
if (command == "cycle-through-linked-images") {
browser.storage.local.get(["linkedImagesRegexp"])
.then((result) => {
prepareAndGoForward(extractLinks, {regexp: result.linkedImagesRegexp});
});
}
if (command == "cycle-through-embedded-images") {
browser.storage.local.get(["minWidth", "minHeight"])
.then((result) => {
prepareAndGoForward(extractImages, {minWidth: result.minWidth, minHeight: result.minHeight});
});
}
if (command == "abort-cycling") {
goToOrigin();
}
});
function extractLinks(argumentObject) {
const regexp = new RegExp(argumentObject.regexp, "i");
const matchingURLs = [];
for (const link of document.links) {
const isMatch = link.href.match(regexp);
const isKnown = matchingURLs.indexOf(link.href) >= 0;
if (isMatch && !isKnown) {
matchingURLs.push(link.href);
}
}
const referers = [];
for (const _url of matchingURLs) {
referers.push([document.URL]);
}
return [matchingURLs, referers];
}
function extractImages(argumentObject) {
const matchingURLs = [];
for (const image of document.images) {
const imageURL = image.src;
const isHighEnough = image.naturalHeight >= argumentObject.minHeight;
const isWideEnough = image.naturalWidth >= argumentObject.minWidth;
const isKnown = matchingURLs.indexOf(imageURL) >= 0;
if (!isKnown && isHighEnough && isWideEnough) {
matchingURLs.push(imageURL);
}
}
const referers = [];
for (const _url of matchingURLs) {
referers.push([document.URL]);
}
return [matchingURLs, referers];
}
function prepareAndGoForward(extractorFunction, extractorFunctionArguments) {
if (!state[currentTab]) {
const getURLsAndReferers = browser.tabs.executeScript(
currentTab,
{
allFrames: true,
code: `
${ extractorFunction.toString() };
${ extractorFunction.name }(${ JSON.stringify(extractorFunctionArguments) })
`,
}
);
const getOrigin = browser.tabs.executeScript(currentTab, {code: "document.URL"});
Promise.all([getURLsAndReferers, getOrigin])
.then((result) => {
const urlsAndReferersResults = result[0];
const originResult = result[1];
const origin = originResult[0];
const urlsAndReferers = mergeFrameResults(urlsAndReferersResults);
const urls = urlsAndReferers[0];
const referers = urlsAndReferers[1];
state[currentTab] = getInitialTabData(urls, referers, origin);
goForward();
});
} else {
goForward();
}
}
function mergeFrameResults(results) {
let urls = [];
let referers = [];
for (const array of results) {
urls = [...urls, ...array[0]];
referers = [...urls, ...array[1]];
}
return [urls, referers];
}
function getInitialTabData(urls, referers, origin) {
if (!urls.length > 0) {
return null;
}
const data = {
index: -1,
origin: origin,
referers: referers,
urls: urls,
};
return data;
}
function goForward() {
const tabData = state[currentTab];
if (!tabData) {
return;
}
const urls = tabData.urls;
const referers = tabData.referers;
const newIndex = tabData.index + 1;
if (newIndex >= urls.length) {
goToOrigin();
return;
}
const url = urls[newIndex];
const referer = referers[newIndex];
function addReferer(details) {
browser.webRequest.onBeforeSendHeaders.removeListener(addReferer);
const requestHeaders = details.requestHeaders;
requestHeaders.push({name: "Referer", value: referer});
return {requestHeaders: requestHeaders};
}
function handleRedirects(details) {
urls[urls.indexOf(details.url)] = details.redirectUrl;
}
browser.webRequest.onBeforeSendHeaders.addListener(addReferer, {urls: [url]}, ["blocking", "requestHeaders"]);
browser.webRequest.onBeforeRedirect.addListener(handleRedirects, {urls: [url]});
browser.tabs.update(currentTab, {url: url});
}
browser.webNavigation.onCommitted.addListener(
(details) => {
const tabData = state[currentTab];
if (!tabData) {
return;
}
if (details.tabId != currentTab || details.frameId != 0) {
return;
}
const index = tabData.urls.indexOf(details.url);
if (index != -1) {
tabData.index = index;
} else {
state[currentTab] = null;
}
}
);
function goToOrigin() {
const tabData = state[currentTab];
if (!tabData) {
return;
}
browser.tabs.update(currentTab, {url: tabData.origin});
}