diff --git a/Plugins/GoogleSearchReplace/GoogleSearchReplace.plugin.js b/Plugins/GoogleSearchReplace/GoogleSearchReplace.plugin.js index 2859b6d508..90028398fd 100644 --- a/Plugins/GoogleSearchReplace/GoogleSearchReplace.plugin.js +++ b/Plugins/GoogleSearchReplace/GoogleSearchReplace.plugin.js @@ -32,13 +32,13 @@ class GoogleSearchReplace { `; this.messageContextSubMenuMarkup = - `
+ `
REPLACE_submenu_disabled_text
- ${Object.keys(this.defaults.engines).map((key, i) => `
${this.defaults.engines[key].name}
`).join("")} + ${Object.keys(this.defaults.engines).map((key, i) => `
${this.defaults.engines[key].name}
`).join("")}
`; } @@ -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 = `
${this.getName()}
`; + let engines = BDFDB.getAllData(this, "engines"); + let settingshtml = `
${this.getName()}
`; settingshtml += `

Search Engines:

`; for (let key in engines) { settingshtml += `

${this.defaults.engines[key].name}

`; @@ -62,7 +62,7 @@ class GoogleSearchReplace { settingshtml += `
`; settingshtml += `
`; - 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