-
Notifications
You must be signed in to change notification settings - Fork 357
/
OpenLinksInDiscord.plugin.js
107 lines (78 loc) · 3.47 KB
/
OpenLinksInDiscord.plugin.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
//META{"name":"OpenLinksInDiscord","website":"https://metalloriff.github.io/toms-discord-stuff/","source":"https://github.com/Metalloriff/BetterDiscordPlugins/blob/master/OpenLinksInDiscord.plugin.js"}*//
class OpenLinksInDiscord {
getName() { return "OpenLinksInDiscord"; }
getDescription() { return "Opens links in a new window in Discord, instead of in your web browser. Hold shift to open links normally."; }
getVersion() { return "1.1.4"; }
getAuthor() { return "Metalloriff"; }
load() {}
start() {
let libLoadedEvent = () => {
try{ this.onLibLoaded(); }
catch(err) { console.error(this.getName(), "fatal error, plugin could not be started!", err); try { this.stop(); } catch(err) { console.error(this.getName() + ".stop()", err); } }
};
let lib = document.getElementById("NeatoBurritoLibrary");
if(!lib) {
lib = document.createElement("script");
lib.id = "NeatoBurritoLibrary";
lib.type = "text/javascript";
lib.src = "https://rawgit.com/Metalloriff/BetterDiscordPlugins/master/Lib/NeatoBurritoLibrary.js";
document.head.appendChild(lib);
}
this.forceLoadTimeout = setTimeout(libLoadedEvent, 30000);
if(typeof window.NeatoLib !== "undefined") libLoadedEvent();
else lib.addEventListener("load", libLoadedEvent);
}
getSettingsPanel() {
let save = () => NeatoLib.Settings.save(this);
setTimeout(() => {
NeatoLib.Settings.pushElements([
NeatoLib.Settings.Elements.createToggleGroup("ld-tg", "Keys", [
{ title : "Control/command", value : "ctrlKey", setValue : this.settings.ctrlKey },
{ title : "Shift", value : "shiftKey", setValue : this.settings.shiftKey },
{ title : "Alt", value : "altKey", setValue : this.settings.altKey }
], c => {
this.settings[c.value] = !this.settings[c.value];
save();
}),
NeatoLib.Settings.Elements.createToggleSwitch("Open in browser by default (holding keys above will open them in Discord)", this.settings.reverse, () => {
this.settings.reverse = !this.settings.reverse;
save();
})
], this.getName());
}, 0);
return NeatoLib.Settings.Elements.pluginNameLabel(this.getName());
}
onLibLoaded() {
NeatoLib.Updates.check(this);
this.settings = NeatoLib.Settings.load(this, {
ctrlKey : false,
shiftKey : true,
altKey : false,
reverse : false
});
this.event = e => {
if(e.target.localName == "a" && e.target.href && e.target.href.startsWith("http") && !e.target.parentElement.className.includes("channel") && !e.target.href.includes("/channels/")) {
if((!this.settings.ctrlKey || e.ctrlKey) && (!this.settings.shiftKey || e.shiftKey) && (!this.settings.altKey || e.altKey)) {
if(this.settings.reverse) {
this.onClickLink(e);
}
} else if(!this.settings.reverse) {
this.onClickLink(e);
}
}
};
document.addEventListener("click", this.event);
this.electron = require("electron");
NeatoLib.Events.onPluginLoaded(this);
}
onClickLink(e) {
let window = new this.electron.remote.BrowserWindow({ frame : true, resizeable : true, show : true, webPreferences : { nodeIntegration : false, nodeIntegrationInWorker : false }});
window.maximize();
window.setMenu(null);
window.loadURL(e.target.href);
e.preventDefault();
}
stop() {
document.removeEventListener("click", this.event);
}
}