-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpageLoad.js
66 lines (54 loc) · 1.74 KB
/
pageLoad.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
// When the field is set on a video, a brief reload is made removing the
// field from the URL but keeping the reverted setting. This set holds
// these urls before they are reset.
videoUrlSet = new Set();
// listen for new tabs
chrome.tabs.onCreated.addListener(function (tabId , info) {
// info may be undefined when tab created
if(typeof(info) != "undefined" && typeof(info.url) != "undefined"){
url = info.url;
if(isYouTubeUrl(url) && doesUrlNotContainField(url) && !videoUrlSet.has(tabId)){
revertYT(tabId);
}
if(info.status === "complete"){
videoUrlSet.delete(tabId);
}
}
});
// listen for updated tabs
chrome.tabs.onUpdated.addListener(function (tabId , info, tab) {
if(info.status === "complete"){
// wait until next reqeust has passed, then clear the set
setTimeout(function(){
videoUrlSet.clear();
}, 2000);
}
// not all tab updates contain the URL (such as title, or status complete)
if(typeof(info) != "undefined" && typeof(info.url) != "undefined"){
url = info.url;
if(isYouTubeUrl(url) && doesUrlNotContainField(url) && !videoUrlSet.has(tab.url)){
revertYT(tabId);
}
}
});
// enable old version
function revertYT(tabId){
chrome.tabs.get(tabId, function(tab){
videoUrlSet.add(tab.url);
if(tab.url.indexOf("?") !== -1){
chrome.tabs.update(tabId, {
url: tab.url + "&disable_polymer=true"
});
}else{
chrome.tabs.update(tabId, {
url: tab.url + "?disable_polymer=true"
});
}
});
}
function doesUrlNotContainField(url){
return url.indexOf("disable_polymer=true") === -1;
}
function isYouTubeUrl(url){
return (url.indexOf("www.youtube.com") !== -1 || url.indexOf("https://youtube.com") !== -1);
}