-
Notifications
You must be signed in to change notification settings - Fork 52
/
ff2mpv.js
193 lines (164 loc) · 4.86 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const contexts = ["link", "image", "video", "audio", "selection", "frame"];
const CREATE_PROFILE = "createProfile";
const UPDATE_PROFILE = "updateProfile";
const DELETE_PROFILE = "deleteProfile";
const PROFILES = "profiles";
const OPEN_VIDEO = "openVideo";
const TITLE = "Play in MPV";
function onError(error) {
console.log(`${error}`);
}
function ff2mpv(url, tabId, options) {
if (tabId != null) {
chrome.scripting.executeScript({
target: { tabId: tabId, allFrames: true },
func: () => {
document.querySelectorAll("video").forEach((video) => video.pause());
},
});
}
options = options ? options : [];
chrome.runtime.sendNativeMessage("ff2mpv", { url, options }).catch(onError);
}
async function getProfiles() {
try {
return (await chrome.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, tab) {
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 tabId = tab ? tab.tabId : null;
const options = await getOptions(info.menuItemId);
ff2mpv(url, tabId, options);
}
}
}
async function changeToMultiEntries() {
// Remove single entry
await chrome.contextMenus.removeAll();
// Add sub context menu
await chrome.contextMenus.create({
id: "ff2mpv",
title: "Profiles",
contexts,
});
await chrome.contextMenus.create({
parentId: "ff2mpv",
id: "22941114-4db3-4296-8fc2-49f178843f52",
title: TITLE,
contexts,
});
}
async function changeToSingleEntry() {
// Remove sub context menu
await chrome.contextMenus.removeAll();
await chrome.contextMenus.create({
id: "ff2mpv",
title: TITLE,
contexts,
});
}
async function createProfile(profile) {
const profiles = await getProfiles();
if (profiles.length === 0) {
await changeToMultiEntries();
}
await chrome.contextMenus.create({
parentId: "ff2mpv",
id: profile.id,
title: profile.name,
contexts,
});
}
async function deleteProfile(menuItemId) {
await chrome.contextMenus.remove(menuItemId);
const profiles = (await getProfiles()).filter((pf) => pf.id !== menuItemId);
if (profiles.length === 0) {
await changeToSingleEntry();
}
}
async function updateProfile(profile) {
await chrome.contextMenus.update(profile.id, {
title: profile.name,
});
}
chrome.action.onClicked.addListener((tab) => {
ff2mpv(tab.url, tab.id);
});
// Messages sent with chrome.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
chrome.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 sendResponse("failure");
}
});
chrome.runtime.onInstalled.addListener(async (_) => {
const profiles = await getProfiles();
if (profiles.length === 0) {
await changeToSingleEntry();
} else {
await changeToMultiEntries();
await profiles.reduce(async (promise, profile) => {
await promise;
await chrome.contextMenus.create({
parentId: "ff2mpv",
id: profile.id,
title: profile.name,
contexts,
});
}, Promise.resolve());
}
});
chrome.contextMenus.onClicked.addListener(submenuClicked);
chrome.runtime.onMessage.addListener(async (request, _sender, sendResponse) => {
if (!request) {
console.warn("No request in message");
return;
}
const { type, profile } = request;
switch (type) {
case CREATE_PROFILE:
await createProfile(profile);
return sendResponse("ok");
case UPDATE_PROFILE:
await updateProfile(profile);
return sendResponse("ok");
case DELETE_PROFILE:
await deleteProfile(profile.id);
return sendResponse("ok");
default:
console.warn("No handler for type:", type);
return sendResponse("failure");
}
});