-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_worker.js
47 lines (40 loc) · 1.38 KB
/
service_worker.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
import { with_style } from './options_load.js';
var tabs = [];
// When the icon next to the address bar is clicked, subtitles start playing.
chrome.action.onClicked.addListener(function(tab) {
tabs.push(tab);
launch_overlay(tab, true);
});
function launch_overlay(tab, interactive) {
with_style_and_inferred_languages(function(style, languages) {
chrome.tabs.sendMessage(tab.id, {
style: style,
languages: languages,
interactive: interactive
});
});
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'options update') {
console.log('will try to update');
tabs.forEach(tab => launch_overlay(tab, false));
}
});
// Load options defined by user: css rules and active subtitle languages.
function with_style_and_inferred_languages(callback) {
with_style(function(style) {
// Infer the subtitle languages from the css rules.
// (This used to be done more properly but regex is sufficient really.)
var languages = [];
var re = /\.most-([A-z]*)/g; // Match things like ".most-en".
var m;
while (m = re.exec(style)) {
var lang = m[1];
if (lang == 'subtitles') continue; // False positive!
if (languages.indexOf(lang) == -1) {
languages.push(lang); // Store things like "en".
}
}
callback(style, languages);
});
}