-
Notifications
You must be signed in to change notification settings - Fork 0
/
overlay.js
91 lines (62 loc) · 2.47 KB
/
overlay.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
/* MoST Subtitle Player :: Implementation Overview
=================================================
Code is inserted into the target web page.
We tap into the existing subtitle system.
A transparent overlay is created as a div.
Subtitles are configurable with CSS in a Chrome options page.
Things were more complicated before. See older commits.
*/
/* Developer's notes
===================
In order to access the global variable 'player' at Viki when debugging using
Chrome's Developer Tools, the 'Elements' tab must be active.
Tip: Use the 'debugger' command to insert a breakpoint!
For notes about the manifest v3 migration, see the commit logs.
*/
$(document).ready(function () {
function init(message, sender, callback) {
$('#most-overlay').remove(); // hacky cleanup of possible old instances
if (message.interactive) {
alert("Hint: Use F11 instead of the fullscreen button to keep dual subtitles in that mode.");
}
addOverlay();
setStyle(message.style);
addCueListeners(message.languages);
}
chrome.runtime.onMessage.addListener(init);
});
function addOverlay(style) {
var overlay = $("<div id='most-overlay'></div>");
$("body").append(overlay);
}
function setStyle(style) {
updateVideoRectangle();
var styleEl = document.createElement("style");
styleEl.innerHTML = style;
$("head").append(styleEl);
// respond to window resize (e.g. F11)
window.addEventListener("resize", updateVideoRectangle);
}
function addCueListeners(languages) {
// Inject listeners (`injected_code.js`) into the webpage.
var script = document.createElement('script');
script.src = chrome.runtime.getURL('injected_code.js');
script.onload = function() { this.remove(); }; // (Not sure what this does...)
script.dataset.params = JSON.stringify({languages: languages});
(document.head || document.documentElement).appendChild(script);
}
function updateVideoRectangle() {
// Get the video player.
var video = document.getElementById("vmplayer_id_html5_api");
var videoRect = video.getBoundingClientRect();
$("#most-overlay").css({
position: "fixed",
width: videoRect.width + "px",
height: videoRect.height + "px",
left: videoRect.left,
top: videoRect.top,
zIndex: "2147483647", // Place subtitles on top of the video.
pointerEvents: "none",
textAlign: "center"
});
}