Skip to content

Commit 1f5f092

Browse files
authored
store version of local .desktop file
2 parents 0d0bb2a + e015411 commit 1f5f092

File tree

6 files changed

+79
-42
lines changed

6 files changed

+79
-42
lines changed

background.js

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,43 @@
11
"use strict";
22

3-
// noinspection ES6ConvertVarToLetConst; will set the variable undefined to undefined if already existing
3+
// change this when mpv-scheme-handler.desktop changes
4+
const desktopFileVersion = 2;
5+
let locallyInstalledVersion = null;
6+
7+
// noinspection ES6ConvertVarToLetConst; will set the variable undefined to undefined if not already existing
48
var browser, chrome;
59
const isChrome = !browser;
610
browser = browser || chrome;
711

812
const menus = browser.menus || browser.contextMenus;
913

10-
function openInMpv(url) {
14+
async function openInMpv(url) {
1115
const mpvUrl = `mpv://watch#${url}`;
1216

13-
browser.tabs.update({ url: mpvUrl }).then(() => {
14-
console.debug("navigating to:", mpvUrl);
15-
}, (error) => {
16-
console.debug("failed " + mpvUrl, error);
17-
});
17+
const alreadySavedDesktop = await localDesktopFileVersionIsCurrent();
18+
if (alreadySavedDesktop) {
19+
await browser.tabs.update({ url: mpvUrl }).then(() => {
20+
console.debug("navigating to:", mpvUrl);
21+
}, (error) => {
22+
console.debug("failed " + mpvUrl, error);
23+
});
24+
} else {
25+
await askToInstall();
26+
await saveCurrentDesktopFileVersion();
27+
}
28+
}
29+
30+
async function localDesktopFileVersionIsCurrent() {
31+
const key = Object.keys({ desktopFileVersion })[0];
32+
const savedVersion = locallyInstalledVersion === null ? (await browser.storage.local.get(key))[key] : locallyInstalledVersion;
33+
console.debug({ savedVersion, deskCache: locallyInstalledVersion, key });
34+
locallyInstalledVersion = savedVersion;
35+
return savedVersion === desktopFileVersion;
36+
}
37+
38+
async function saveCurrentDesktopFileVersion() {
39+
locallyInstalledVersion = desktopFileVersion;
40+
return await browser.storage.local.set({ desktopFileVersion });
1841
}
1942

2043
menus.create({
@@ -31,15 +54,19 @@ menus.onClicked.addListener((info, tab) => {
3154
switch (info.menuItemId) {
3255
case "openInMpv":
3356
const url = info.linkUrl || info.srcUrl || info.selectionText || info.frameUrl || info.pageUrl;
34-
if (url) openInMpv(url); else console.debug({ info: info, tab: tab });
57+
if (url) return openInMpv(url); else console.debug({ info: info, tab: tab });
3558
break;
3659
}
3760
});
3861

3962
browser.action.onClicked.addListener((tab) => {
40-
openInMpv(tab.url);
63+
return openInMpv(tab.url);
4164
});
4265

66+
function askToInstall() {
67+
return browser.tabs.create({ url: "setup.sh" });
68+
}
69+
4370
const filter = {
4471
url: [{
4572
schemes: ["mpv", "mpvx"],
@@ -60,7 +87,7 @@ if (!isChrome) {
6087
console.debug("onErrorOccurred");
6188
console.debug(details);
6289

63-
browser.tabs.create({ url: "setup.sh" });
90+
askToInstall();
6491
}
6592
}, filter);
6693
}

manifest-chrome.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "__MSG_extensionName__",
33
"short_name": "__MSG_extensionShortName__",
44
"description": "__MSG_extensionDescription__",
5-
"version": "2.0.0",
5+
"version": "2.1.0",
66
"manifest_version": 3,
77
"default_locale": "en",
88
"icons": {
@@ -26,6 +26,7 @@
2626
"permissions": [
2727
"contextMenus",
2828
"activeTab",
29+
"storage",
2930
"webNavigation"
3031
]
3132
}

manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "__MSG_extensionName__",
33
"short_name": "__MSG_extensionShortName__",
44
"description": "__MSG_extensionDescription__",
5-
"version": "2.0.0",
5+
"version": "2.1.0",
66
"manifest_version": 3,
77
"default_locale": "en",
88
"icons": {
@@ -28,6 +28,7 @@
2828
"permissions": [
2929
"menus",
3030
"activeTab",
31+
"storage",
3132
"webNavigation"
3233
],
3334
"browser_specific_settings": {

mpv-scheme-handler.desktop

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ Comment=Launch MPV
1111
Hidden=false
1212

1313
# copy to ~/.local/share/applications/mpv-scheme-handler.desktop
14+
# Version 2
15+
# update this version in background.js and open-in-mpv.plugin.js

open-in-mpv.plugin.js

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22
* @name open in mpv
33
* @author binarynoise
44
* @description Use the context menu to open a video in mpv.
5-
* @version 2.0.0
5+
* @version 2.1.0
66
*/
77

88
'use strict';
9+
const electron = require('electron');
910

10-
const settings = { showAgain: true };
11+
// change this when mpv-scheme-handler.desktop changes
12+
const desktopFileVersion = 2;
13+
14+
const settings = {
15+
locallyInstalledVersion: null
16+
};
1117

1218
const MPVSchemePrefix = "mpv://watch#";
1319

@@ -21,33 +27,30 @@ function contextMenuPatch(tree, context) {
2127
tree.props.children.push(BdApi.ContextMenu.buildItem({
2228
type: "text", label: "open in mpv", action: () => {
2329
console.log("link is " + href);
24-
const electron = require('electron');
25-
26-
electron.shell.openExternal(MPVSchemePrefix + href).then(() => {
27-
if (settings.showAgain !== false) {
28-
BdApi.UI.showConfirmationModal("Open in mpv",
29-
"Successfully opened " + href + " in mpv. Did nothing happen? Download and run setup.sh",
30-
{
31-
confirmText: "Download setup.sh", onConfirm: () => {
32-
electron.shell.openExternal('https://raw.githubusercontent.com/binarynoise/open-in-mpv/main/setup.sh');
33-
},
34-
35-
cancelText: "Ok, don't show again", onCancel: () => {
36-
settings.showAgain = false;
37-
BdApi.Data.save("open-in-mpv", "settings", settings);
38-
},
39-
})
40-
} else {
30+
31+
if (settings.locallyInstalledVersion && settings.locallyInstalledVersion >= desktopFileVersion) {
32+
33+
electron.shell.openExternal(MPVSchemePrefix + href).then(() => {
4134
BdApi.UI.showToast("" + href + " opened in mpv.", { type: "success" });
42-
}
43-
console.log("success");
44-
}, (error) => {
45-
console.log(`failed to open ${MPVSchemePrefix}href`);
46-
console.log(error);
47-
}).catch((error) => {
48-
console.log(`failed to open ${MPVSchemePrefix}href`);
49-
console.log(error);
50-
})
35+
console.log("success");
36+
}, (error) => {
37+
console.log(`failed to open ${MPVSchemePrefix}href`);
38+
console.log(error);
39+
}).catch((error) => {
40+
console.log(`failed to open ${MPVSchemePrefix}href`);
41+
console.log(error);
42+
})
43+
} else {
44+
BdApi.UI.showConfirmationModal("Open in mpv",
45+
"Open in mpv was updated or freshly installed. Please download and run setup.sh (again).",
46+
{
47+
confirmText: "Download setup.sh", onConfirm: () => {
48+
electron.shell.openExternal("https://raw.githubusercontent.com/binarynoise/open-in-mpv/main/setup.sh");
49+
settings.locallyInstalledVersion = desktopFileVersion;
50+
BdApi.Data.save("open-in-mpv", "settings", settings);
51+
},
52+
})
53+
}
5154
},
5255
}))
5356
}
@@ -89,7 +92,7 @@ module.exports = () => ({
8992
BdApi.ContextMenu.patch("message", contextMenuPatch);
9093
}, stop() {
9194
BdApi.ContextMenu.unpatch("message", contextMenuPatch);
92-
}, getSettingsPanel: () => React.createElement(SettingComponent),
95+
}, //getSettingsPanel: () => React.createElement(SettingComponent),
9396
});
9497

9598
function isEmpty(obj) {

setup.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/bin/sh
2-
# one last thing to do: to use the extension, run the following commands in your terminal:
2+
# When the addon is installed or updated, there is one last thing to do.
3+
# To use the extension, run the following commands in your terminal:
34

45
set -e
56

@@ -10,3 +11,5 @@ else
1011
fi
1112

1213
xdg-mime default mpv-scheme-handler.desktop x-scheme-handler/mpv
14+
15+
set +e

0 commit comments

Comments
 (0)