-
Notifications
You must be signed in to change notification settings - Fork 5
/
contentscript.js
115 lines (100 loc) · 3.96 KB
/
contentscript.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
(function() {
const STORAGE_KEY = "video-volume";
const MAX_VOL_STORAGE_KEY = "max-global-video-volume";
const STATUS_KEY = "video-volume-status-per-website";
let store = chrome.storage || window.storage;
let runtime = chrome.runtime || window.runtime;
runtime.onMessage.addListener(function(volume) {
let host = window.location.host;
data = {};
data[STORAGE_KEY + '_' + host] = Math.round(volume);
saveVolumne(data);
});
lock = false;
function setVolume() {
let host = window.location.host;
let key = STATUS_KEY + '_' + host;
let websiteKey = STORAGE_KEY + '_' + host;
store.local.get([key, MAX_VOL_STORAGE_KEY, websiteKey, STORAGE_KEY], function(data) {
var videoTags = Array.from(document.getElementsByTagName("video"));
var audioTags = Array.from(document.getElementsByTagName("audio"));
var iframes = document.getElementsByTagName('iframe')
if (iframes.length) {
for (var x = 0; x < iframes.length; x++) {
var iframe = iframes[x];
if (iframe.contentDocument) {
videoTags = videoTags.concat(Array.from(iframe.contentDocument.getElementsByTagName('video')))
audioTags = audioTags.concat(Array.from(iframe.contentDocument.getElementsByTagName('audio')))
}
}
}
if (videoTags.length == 0 && audioTags.length == 0) {
return;
}
if (lock) {
return;
}
let maxVol = data[MAX_VOL_STORAGE_KEY] || 0.5;
for (var x = 0; x < videoTags.length; x++) {
if (websiteKey in data && key in data && data[key] === true) {
videoTags[x].volume = data[websiteKey] / 100;
} else if (maxVol / 100 < videoTags[x].volume) {
videoTags[x].volume = maxVol / 100;
}
}
for (var x = 0; x < audioTags.length; x++) {
if (websiteKey in data && key in data && data[key] === true) {
audioTags[x].volume = data[websiteKey] / 100;
} else if (maxVol / 100 < audioTags[x].volume) {
audioTags[x].volume = maxVol / 100;
}
}
for (var i in videoTags) {
if (!videoTags[i]) {
return;
}
if (!(key in data) || !data[key]) {
return;
}
videoTags[i].onvolumechange = function (argument) {
if (argument.target.muted || argument.target.paused) {
return;
}
lock = true;
var key = {};
key[STORAGE_KEY + '_' + host] = Math.round(argument.target.volume * 100)
saveVolumne(key);
}
}
for (var i in audioTags) {
if (!audioTags[i]) {
return;
}
if (!(key in data) || !data[key]) {
return;
}
audioTags[i].onvolumechange = function (argument) {
if (argument.target.muted || argument.target.paused) {
return;
}
lock = true;
var key = {};
key[STORAGE_KEY + '_' + host] = Math.round(argument.target.volume * 100);
saveVolumne(key);
}
}
});
}
function saveVolumne(data) {
store.local.set(data, function() {
lock = false;
});
}
function parseHostFromURL(url) {
var parser = document.createElement('a');
parser.href = url;
return parser.host;
}
setVolume();
window.setInterval(setVolume, 500);
})();