GoogleSearchReplace now uses patching instead of observing

This commit is contained in:
Mirco Wittrien 2019-01-03 10:45:42 +01:00
parent 32c0893a5d
commit 90c1493599
1 changed files with 40 additions and 73 deletions

View File

@ -32,13 +32,13 @@ class GoogleSearchReplace {
</div>`;
this.messageContextSubMenuMarkup =
`<div class="${BDFDB.disCN.contextmenu} googleReplaceSearchSubMenu">
`<div class="${BDFDB.disCN.contextmenu} googlereplacesearch-submenu">
<div class="${BDFDB.disCN.contextmenuitemgroup}">
<div class="${BDFDB.disCN.contextmenuitem} alldisabled-item ${BDFDB.disCN.contextmenuitemdisabled}">
<span class="DevilBro-textscrollwrapper" speed=3><div class="DevilBro-textscroll">REPLACE_submenu_disabled_text</div></span>
<div class="${BDFDB.disCN.contextmenuhint}"></div>
</div>
${Object.keys(this.defaults.engines).map((key, i) => `<div engine="${key}" class="${BDFDB.disCN.contextmenuitem} GRS-item"><span>${this.defaults.engines[key].name}</span><div class="${BDFDB.disCN.contextmenuhint}"></div></div>`).join("")}
${Object.keys(this.defaults.engines).map((key, i) => `<div engine="${key}" class="${BDFDB.disCN.contextmenuitem} GRS-item"><span class="DevilBro-textscrollwrapper" speed=3><div class="DevilBro-textscroll">${this.defaults.engines[key].name}</div></span><div class="${BDFDB.disCN.contextmenuhint}"></div></div>`).join("")}
</div>
</div>`;
}
@ -47,14 +47,14 @@ class GoogleSearchReplace {
getDescription () {return "Replaces the default Google Text Search with a selection menu of several search engines.";}
getVersion () {return "1.1.4";}
getVersion () {return "1.1.5";}
getAuthor () {return "DevilBro";}
getSettingsPanel () {
if (!this.started || typeof BDFDB !== "object") return;
var engines = BDFDB.getAllData(this, "engines");
var settingshtml = `<div class="${this.getName()}-settings DevilBro-settings"><div class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.title + BDFDB.disCNS.size18 + BDFDB.disCNS.height24 + BDFDB.disCNS.weightnormal + BDFDB.disCN.marginbottom8}">${this.getName()}</div><div class="DevilBro-settings-inner">`;
let engines = BDFDB.getAllData(this, "engines");
let settingshtml = `<div class="${this.getName()}-settings DevilBro-settings"><div class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.title + BDFDB.disCNS.size18 + BDFDB.disCNS.height24 + BDFDB.disCNS.weightnormal + BDFDB.disCN.marginbottom8}">${this.getName()}</div><div class="DevilBro-settings-inner">`;
settingshtml += `<div class="${BDFDB.disCNS.flex + BDFDB.disCNS.flex2 + BDFDB.disCNS.horizontal + BDFDB.disCNS.horizontal2 + BDFDB.disCNS.directionrow + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCNS.nowrap + BDFDB.disCN.marginbottom8}" style="flex: 1 1 auto;"><h3 class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.title + BDFDB.disCNS.marginreset + BDFDB.disCNS.weightmedium + BDFDB.disCNS.size16 + BDFDB.disCNS.height24 + BDFDB.disCN.flexchild}" style="flex: 0 0 auto;">Search Engines:</h3></div><div class="DevilBro-settings-inner-list">`;
for (let key in engines) {
settingshtml += `<div class="${BDFDB.disCNS.flex + BDFDB.disCNS.flex2 + BDFDB.disCNS.horizontal + BDFDB.disCNS.horizontal2 + BDFDB.disCNS.directionrow + BDFDB.disCNS.justifystart + BDFDB.disCNS.aligncenter + BDFDB.disCNS.nowrap + BDFDB.disCN.marginbottom8}" style="flex: 1 1 auto;"><h3 class="${BDFDB.disCNS.titledefault + BDFDB.disCNS.title + BDFDB.disCNS.marginreset + BDFDB.disCNS.weightmedium + BDFDB.disCNS.size16 + BDFDB.disCNS.height24 + BDFDB.disCN.flexchild}" style="flex: 1 1 auto;">${this.defaults.engines[key].name}</h3><div class="${BDFDB.disCNS.flexchild + BDFDB.disCNS.switchenabled + BDFDB.disCNS.switch + BDFDB.disCNS.switchvalue + BDFDB.disCNS.switchsizedefault + BDFDB.disCNS.switchsize + BDFDB.disCN.switchthemedefault}" style="flex: 0 0 auto;"><input type="checkbox" value="${key}" class="${BDFDB.disCNS.switchinnerenabled + BDFDB.disCN.switchinner}"${engines[key] ? " checked" : ""}></div></div>`;
@ -62,7 +62,7 @@ class GoogleSearchReplace {
settingshtml += `</div>`;
settingshtml += `</div></div>`;
var settingspanel = $(settingshtml)[0];
let settingspanel = $(settingshtml)[0];
BDFDB.initElements(settingspanel);
@ -76,9 +76,8 @@ class GoogleSearchReplace {
load () {}
start () {
var libraryScript = null;
if (typeof BDFDB !== "object" || BDFDB.isLibraryOutdated()) {
if (typeof BDFDB === "object") BDFDB = "";
let libraryScript = null;
if (typeof BDFDB !== "object" || typeof BDFDB.isLibraryOutdated !== "function" || BDFDB.isLibraryOutdated()) {
libraryScript = document.querySelector('head script[src="https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js"]');
if (libraryScript) libraryScript.remove();
libraryScript = document.createElement("script");
@ -87,30 +86,13 @@ class GoogleSearchReplace {
document.head.appendChild(libraryScript);
}
this.startTimeout = setTimeout(() => {this.initialize();}, 30000);
if (typeof BDFDB === "object") this.initialize();
if (typeof BDFDB === "object" && typeof BDFDB.isLibraryOutdated === "function") this.initialize();
else libraryScript.addEventListener("load", () => {this.initialize();});
}
initialize () {
if (typeof BDFDB === "object") {
BDFDB.loadMessage(this);
var observer = null;
observer = new MutationObserver((changes, _) => {
changes.forEach(
(change, i) => {
if (change.addedNodes) {
change.addedNodes.forEach((node) => {
if (node.nodeType == 1 && node.className.includes(BDFDB.disCN.contextmenu)) {
this.onContextMenu(node);
}
});
}
}
);
});
BDFDB.addObserver(this, BDFDB.dotCN.appmount, {name:"messageContextObserver",instance:observer}, {childList: true});
}
else {
console.error(this.getName() + ": Fatal Error: Could not load BD functions!");
@ -126,8 +108,8 @@ class GoogleSearchReplace {
// begin of own functions
updateSettings (settingspanel) {
var settings = {};
for (var input of settingspanel.querySelectorAll(BDFDB.dotCN.switchinner)) {
let settings = {};
for (let input of settingspanel.querySelectorAll(BDFDB.dotCN.switchinner)) {
settings[input.value] = input.checked;
}
BDFDB.saveAllData(settings, this, "engines");
@ -139,54 +121,39 @@ class GoogleSearchReplace {
this.messageContextSubMenuMarkup = this.messageContextSubMenuMarkup.replace("REPLACE_submenu_disabled_text", this.labels.submenu_disabled_text);
}
onContextMenu (context) {
if (!context || !context.tagName || !context.parentElement || context.querySelector(".googlereplacesearch-item")) return;
for (let group of context.querySelectorAll(BDFDB.dotCN.contextmenuitemgroup)) {
if (BDFDB.getKeyInformation({"node":group, "key":"handleSearchWithGoogle"})) {
var text = BDFDB.getKeyInformation({"node":group, "key":"value"});
if (text) {
$(group).find(BDFDB.dotCN.contextmenuitem).hide();
$(group).append(this.messageContextEntryMarkup)
.on("mouseenter", ".googlereplacesearch-item", (e) => {
this.createContextSubMenu(text, e, context);
});
BDFDB.updateContextPosition(context);
}
break;
onMessageContextMenu (instance, menu) {
console.log(instance);
if (instance.props && instance.props.message && instance.props.channel && instance.props.target && !menu.querySelector(".googlereplacesearch-item")) {
let text = document.getSelection().toString();
let searchinstance = BDFDB.getOwnerInstance({node:menu,props:["handleSearchWithGoogle"]});
if (text && searchinstance && searchinstance._reactInternalFiber && searchinstance._reactInternalFiber.return && searchinstance._reactInternalFiber.return.stateNode) {
$(this.messageContextEntryMarkup).appendTo(searchinstance._reactInternalFiber.return.stateNode)
.on("mouseenter", (e) => {
let messageContextSubMenu = $(this.messageContextSubMenuMarkup)[0];
let engines = BDFDB.getAllData(this, "engines");
$(messageContextSubMenu)
.on("click", ".GRS-item", (e2) => {
instance._reactInternalFiber.return.memoizedProps.closeContextMenu();
let engine = e2.currentTarget.getAttribute("engine");
console.log(engine);
if (engine == "_all") {
for (let key in engines) {
if (key != "_all" && engines[key]) {
window.open(this.defaults.engines[key].url.replace(this.textUrlReplaceString, encodeURIComponent(text)), "_blank");
}
}
}
else window.open(this.defaults.engines[engine].url.replace(this.textUrlReplaceString, encodeURIComponent(text)), "_blank");
});
for (let key in engines) if (!engines[key]) BDFDB.removeEles(messageContextSubMenu.querySelector("[engine='" + key + "']"));
if (messageContextSubMenu.querySelector(".GRS-item")) BDFDB.removeEles(messageContextSubMenu.querySelector(".alldisabled-item"));
BDFDB.appendSubMenu(e.currentTarget, messageContextSubMenu);
});
searchinstance._reactInternalFiber.return.stateNode.firstElementChild.style.setProperty("display", "none", "important");
}
}
}
createContextSubMenu (text, e, context) {
var messageContextSubMenu = $(this.messageContextSubMenuMarkup);
messageContextSubMenu
.on("click", ".GRS-item", (e2) => {
$(context).hide();
var engine = e2.currentTarget.getAttribute("engine");
if (engine == "_all") {
var engines = BDFDB.getAllData(this, "engines");
for (let key in engines) {
if (key != "_all" && engines[key]) window.open(this.defaults.engines[key].url.replace(this.textUrlReplaceString, encodeURIComponent(text)), "_blank");
}
}
else {
window.open(this.defaults.engines[engine].url.replace(this.textUrlReplaceString, encodeURIComponent(text)), "_blank");
}
});
var engines = BDFDB.getAllData(this, "engines");
for (let key in engines) {
if (!engines[key]) messageContextSubMenu.find("[engine='" + key + "']").remove();
}
if (messageContextSubMenu.find(".GRS-item").length > 0) {
messageContextSubMenu.find(".alldisabled-item").remove();
}
BDFDB.appendSubMenu(e.currentTarget, messageContextSubMenu);
}
setLabelsByLanguage () {
switch (BDFDB.getDiscordLanguage().id) {
case "hr": //croatian