-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
76 lines (65 loc) · 2.24 KB
/
Copy pathbackground.js
File metadata and controls
76 lines (65 loc) · 2.24 KB
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
var videoUrls = {};
function checkIfVideoUrl(requestDetails) {
videoUrls[requestDetails.tabId] = [requestDetails.url];
return {
cancel: true
};
}
function messageRecieved(request, sender, sendResponse) {
if (request.parsedUrls) {
videoUrls[sender.tab.id] = videoUrls[sender.tab.id] ? videoUrls[sender.tab.id] : [];
if (videoUrls[sender.tab.id].length > 0) {
videoUrls[sender.tab.id] = videoUrls[sender.tab.id].concat(request.parsedUrls);
} else {
videoUrls[sender.tab.id] = request.parsedUrls;
}
} else if (request.geturls) {
sendResponse({
videoUrls: videoUrls[request.tabId]
});
}
}
function callPageToParseForVideos(tabId, changeInfo, tab) {
browser.storage.local.get("settings").then(function(result) {
var settings = result.settings;
if (settings.host_to_intercept) {
var host_to_intercept = settings.host_to_intercept.split(',');
var host_to_intercept_matches = [];
for (var i = 0; i < host_to_intercept.length; i++) {
host_to_intercept_matches.push("*://*." + host_to_intercept[i] + "/*");
}
browser.webRequest.onBeforeRequest.addListener(
checkIfVideoUrl, {
urls: host_to_intercept_matches,
types: ["media"]
}, ["blocking"]
);
}
if (settings.hosts_to_parse_html) {
_callPageToParseForVideos(tabId, changeInfo, tab);
}
if (settings.hosts_to_show_list) {
_checkToShowPageAction(tab.url, tabId);
}
function _checkToShowPageAction(url, tabId) {
var hosts_to_show_list = settings.hosts_to_show_list.split(',').join("|");
var re = new RegExp(hosts_to_show_list, "i");
if (url.match(re) != null) {
browser.pageAction.show(tabId);
} else {
browser.pageAction.hide(tabId);
}
}
function _callPageToParseForVideos(tabId, changeInfo, tab) {
var hosts_to_parse_html = settings.hosts_to_parse_html.split(',').join("|");
var re = new RegExp(hosts_to_parse_html, "i");
if (tab.url.match(re) != null) {
browser.tabs.executeScript(tabId, {
file: "/parseHtmlForVideo.js"
});
}
}
});
}
browser.tabs.onUpdated.addListener(callPageToParseForVideos);
browser.runtime.onMessage.addListener(messageRecieved);