-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackground.js
More file actions
100 lines (84 loc) · 3.35 KB
/
Copy pathbackground.js
File metadata and controls
100 lines (84 loc) · 3.35 KB
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
"use strict";
// change this when mpv-scheme-handler.desktop changes
const desktopFileVersion = 3;
let locallyInstalledVersion = null;
// noinspection ES6ConvertVarToLetConst; will set the variable undefined to undefined if not already existing
var browser, chrome;
const isChrome = !browser;
browser = browser || chrome;
const menus = browser.menus || browser.contextMenus;
function createMpvSchemeURI(url) {
const decodedURL = decodeURI(url);
const encodedURL = encodeURI(decodedURL).replace(/'/g, "%27");
console.debug({url, encodedURL, same: decodedURL === decodeURI(encodedURL)});
return `mpv://watch#${encodedURL}`;
}
async function openInMpv(url) {
const mpvUrl = createMpvSchemeURI(url);
const alreadySavedDesktop = await localDesktopFileVersionIsCurrent();
if (alreadySavedDesktop) {
await browser.tabs.update({ url: mpvUrl }).then(() => {
console.debug("navigating to:", mpvUrl);
}, (error) => {
console.debug("failed " + mpvUrl, error);
});
} else {
await askToInstall();
await saveCurrentDesktopFileVersion();
}
}
async function localDesktopFileVersionIsCurrent() {
const key = Object.keys({ desktopFileVersion })[0];
const savedVersion = locallyInstalledVersion === null ? (await browser.storage.local.get(key))[key] : locallyInstalledVersion;
console.debug({ savedVersion, deskCache: locallyInstalledVersion, key });
locallyInstalledVersion = savedVersion;
return savedVersion === desktopFileVersion;
}
async function saveCurrentDesktopFileVersion() {
locallyInstalledVersion = desktopFileVersion;
return await browser.storage.local.set({ desktopFileVersion });
}
menus.create({
id: "openInMpv", //
title: browser.i18n.getMessage("extensionName"), //
// chrome: action, all, audio, browser_action, editable, frame, image, launcher, link, page, page_action, selection, video
// firefox: "all", "audio", "bookmark", "editable", "frame", "image", "launcher", "link", "page", "password", "selection", "tab", "tools_menu", "video"
contexts: isChrome ?
["action", "audio", "browser_action", "frame", "image", "link", "page_action", "selection", "video",] :
["action", "audio", "frame", "image", "link", "selection", "tab", "tools_menu", "video",],
});
menus.onClicked.addListener((info, tab) => {
switch (info.menuItemId) {
case "openInMpv":
const url = info.linkUrl || info.srcUrl || info.selectionText || info.frameUrl || info.pageUrl;
if (url) return openInMpv(url); else console.debug({ info: info, tab: tab });
break;
}
});
browser.action.onClicked.addListener((tab) => {
return openInMpv(tab.url);
});
function askToInstall() {
return browser.tabs.create({ url: "setup.sh" });
}
const filter = {
url: [{
schemes: ["mpv", "mpvx"],
}, // {
// urlPrefix: browser.runtime.getURL("/"),
// },
],
};
if (!isChrome) {
browser.webNavigation.onErrorOccurred.addListener((details) => {
// Error code 2152398865 -> kNoContent (mpv)
// Error code 2152398866 -> kUnknownProtocol
if (details.error.endsWith("2152398865")) {
console.debug("opened in mpv");
} else {
console.debug("onErrorOccurred");
console.debug(details);
askToInstall();
}
}, filter);
}