-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathopen-in-mpv.plugin.js
More file actions
110 lines (97 loc) · 4.09 KB
/
Copy pathopen-in-mpv.plugin.js
File metadata and controls
110 lines (97 loc) · 4.09 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
101
102
103
104
105
106
107
108
109
110
/**
* @name open in mpv
* @author binarynoise
* @description Use the context menu to open a video in mpv.
* @version 2.4.0
* @source https://github.com/binarynoise/open-in-mpv
* @donate https://paypal.me/binarynoise
*/
'use strict';
// change this when mpv-scheme-handler.desktop changes
const desktopFileVersion = 3;
const settings = {
locallyInstalledVersion: null
};
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}`;
}
function contextMenuPatch(tree, context) {
// console.debug(tree, context)
let href = [context.target.href, context.target.parentNode?.href].find(e => e && !/^https:\/\/(\w+\.)?discord.com\//.test(e))
if (href === undefined) {
const attachments = context.message.attachments
if (attachments) {
const filtered = attachments.map(e => e.url).filter(e => e);
if (filtered && filtered.length === 1) {
href = filtered[0]
}
}
}
if (href === undefined) {
const embeds = context.message.embeds
if (embeds) {
const filtered = embeds.map(e => e.video || e.image || e).filter(e => e).map(e => e.contentType !== "text/html" && e.url).filter(e => e);
const unique = filtered.filter((value, index, array) => array.indexOf(value) === index)
if (unique && unique.length === 1) {
href = unique[0]
}
}
}
if (href !== undefined) {
const children = tree.props.children?.props?.children || tree.props.children
children.push(BdApi.ContextMenu.buildItem({
type: "separator",
}))
children.push(BdApi.ContextMenu.buildItem({
type: "text", label: "open in mpv", action: () => {
console.log("open-in-mpv: link is " + href);
if (settings.locallyInstalledVersion && settings.locallyInstalledVersion >= desktopFileVersion) {
const newWindow = window.open(createMpvSchemeURI(href), "_blank", "noopener noreferrer");
if (newWindow === null) { // is null because opens in external application
BdApi.UI.showToast(`${href} opened in mpv.`, { type: "success" });
console.log("open-in-mpv: success");
} else {
BdApi.UI.showToast(`${href} failed to open in mpv.`, { type: "error" });
console.log(`open-in-mpv: failed to open ${createMpvSchemeURI(href)} in mpv.`);
}
} else {
BdApi.UI.showConfirmationModal("Open in mpv",
"Open in mpv was updated or freshly installed. Please download and run setup.sh (again).",
{
confirmText: "Download setup.sh", onConfirm: () => {
downloadSetup();
},
}
)
}
},
}))
}
}
function downloadSetup() {
const newWindow = window.open("https://raw.githubusercontent.com/binarynoise/open-in-mpv/main/setup.sh", "_blank", "noopener noreferrer",);
if (newWindow === null) { // is null because opens in external browser
settings.locallyInstalledVersion = desktopFileVersion;
BdApi.Data.save("open-in-mpv", "settings", settings);
}
}
module.exports = () => ({
start() {
const saved = BdApi.Data.load("open-in-mpv", "settings");
if (saved && !isEmpty(saved)) Object.assign(settings, saved);
BdApi.ContextMenu.patch("message", contextMenuPatch);
}, stop() {
BdApi.ContextMenu.unpatch("message", contextMenuPatch);
}, //getSettingsPanel: () => React.createElement(SettingComponent),
});
function isEmpty(obj) {
for (const prop in obj) {
if (Object.prototype.hasOwnProperty.call(obj, prop)) {
return false;
}
}
return true
}