/
ff2mpv.js
173 lines (145 loc) · 4 KB
/
ff2mpv.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const contexts = ["link", "image", "video", "audio", "selection", "frame"];
function onError(error) {
console.log(`${error}`);
}
const OPEN_VIDEO = 'openVideo';
let TITLE;
function ff2mpv(url, options = []) {
browser.tabs.executeScript({
code: "video = document.getElementsByTagName('video');video[0].pause();"
});
browser.runtime.sendNativeMessage("ff2mpv", { url, options }).catch(onError);
}
async function getOS() {
return browser.runtime.getPlatformInfo().then((i) => i.os);
}
async function getProfiles() {
try {
return (await browser.storage.sync.get('profiles'))['profiles'] || [];
} catch (error) {
console.debug('Unable to get profiles:', error);
return [];
}
};
async function getOptions(id) {
try {
const profiles = await getProfiles();
const profile = profiles.find(pf => pf.id === id);
// If profile, remove empty lines
return profile
? profile.content.filter(line => !!line)
: [];
} catch (error) {
console.debug('Unable to get options for profile:', id, error);
return [];
}
}
async function submenuClicked(info) {
if (info.parentMenuItemId === 'ff2mpv' || info.menuItemId === 'ff2mpv') {
/* These should be mutually exclusive, but,
if they aren't, this is a reasonable priority.
*/
const url = info.linkUrl || info.srcUrl || info.selectionText || info.frameUrl;
if (url) {
const options = await getOptions(info.menuItemId);
ff2mpv(url, options);
}
}
}
function changeToMultiEntries() {
// Remove single entry
try {
browser.contextMenus.remove('ff2mpv');
} catch (error) {}
// Add sub context menu
browser.contextMenus.create({
id: "ff2mpv",
title: "Profiles",
contexts,
onclick: submenuClicked,
});
browser.contextMenus.create({
parentId: "ff2mpv",
title: TITLE,
contexts,
onclick: submenuClicked,
});
}
function changeToSingleEntry() {
// Remove sub context menu
try {
browser.contextMenus.remove('ff2mpv');
} catch (error) {}
// Add single entry
browser.contextMenus.create({
id: "ff2mpv",
title: TITLE,
contexts,
onclick: submenuClicked,
});
}
async function createProfile(profile) {
const profiles = await getProfiles();
if (profiles.length === 0) {
changeToMultiEntries();
}
browser.contextMenus.create({
parentId: "ff2mpv",
id: profile.id,
title: profile.name,
contexts,
onclick: submenuClicked,
})
}
async function deleteProfile(menuItemId) {
browser.contextMenus.remove(menuItemId);
const profiles = (await getProfiles())
.filter(pf => pf.id !== menuItemId);
if (profiles.length === 0) {
changeToSingleEntry();
}
}
function updateProfile(profile) {
browser.contextMenus.update(profile.id, {
title: profile.name,
});
}
browser.browserAction.onClicked.addListener((tab) => {
ff2mpv(tab.url);
});
// Messages sent with browser.runtime.sendMessage (https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/sendMessage) from external applications will be handle here.
// ref: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onMessageExternal
browser.runtime.onMessageExternal.addListener((request, sender, sendResponse) => {
if (!request) {
console.warn('No request in external message');
return;
}
const { type, url } = request;
console.debug('Request from:', sender);
switch (type) {
case OPEN_VIDEO:
ff2mpv(url);
return sendResponse('ok');
default:
console.warn('No handler for external type:', type);
return;
}
});
getOS().then(async (os) => {
TITLE = os === "win" ? "Play in MP&V" : "Play in MPV (&W)";
const profiles = await getProfiles();
if (profiles.length === 0) {
changeToSingleEntry();
} else {
changeToMultiEntries();
profiles.forEach(profile => {
browser.contextMenus.create({
parentId: "ff2mpv",
id: profile.id,
title: profile.name,
contexts,
onclick: submenuClicked,
})
});
}
});