module.exports = (Plugin, Api, Vendor) => { if (typeof BDFDB !== "object") global.BDFDB = {$: Vendor.$, BDv2Api: Api}; const {$} = Vendor; return class extends Plugin { initConstructor () { this.css = ` .BSP-pagination-button { background: url('data:image/svg+xml; utf8, ') 50%/9px 12px no-repeat; border: 1px solid rgba(79,84,92,.16); border-radius: 2px; cursor: pointer; height: 18px; left: 20px; opacity: .7; top: 20px; width: 18px; } ${BDFDB.dotCN.themedark} .BSP-pagination-button { background-image: url('data:image/svg+xml; utf8, '); border: 1px solid hsla(0,0%,100%,.16); } .BSP-pagination-button.BSP-pagination-first { -webkit-transform: rotate(180deg); margin-right: 10px; transform: rotate(180deg); } .BSP-pagination-button.BSP-pagination-last { margin-left: 10px; margin-right: 10px; } .BSP-pagination-button${BDFDB.dotCN.searchresultspaginationdisabled} { cursor: default; opacity: .3; } .BSP-pagination-button:not(${BDFDB.dotCN.searchresultspaginationdisabled}):hover { opacity: 1; } `; this.defaults = { settings: { addFirstLast: {value:true, description:"Adds a first and last page button."}, addJumpTo: {value:true, description:"Adds a jump to input field (press enter to jump)."}, cloneToTheTop: {value:true, description:"Clones the controls to the top of the results page."} } }; } onStart () { var 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"); libraryScript.setAttribute("type", "text/javascript"); libraryScript.setAttribute("src", "https://mwittrien.github.io/BetterDiscordAddons/Plugins/BDFDB.js"); document.head.appendChild(libraryScript); } this.startTimeout = setTimeout(() => {this.initialize();}, 30000); if (typeof BDFDB === "object" && typeof BDFDB.isLibraryOutdated === "function") this.initialize(); else libraryScript.addEventListener("load", () => {this.initialize();}); return true; } initialize () { if (typeof BDFDB === "object") { BDFDB.loadMessage(this); this.SearchNavigation = BDFDB.WebModules.findByProperties("searchNextPage","searchPreviousPage"); this.SearchUtils = BDFDB.WebModules.findByProperties("getCurrentSearchId"); var observer = null; observer = new MutationObserver((changes, _) => { changes.forEach( (change, i) => { if (change.addedNodes) { change.addedNodes.forEach((node) => { if (node && node.tagName && node.classList.contains(BDFDB.disCN.searchresultswrap)) { BDFDB.addObserver(this, node, {name:"searchResultsObserver"}, {childList:true, subtree:true}); } }); } } ); }); BDFDB.addObserver(this, BDFDB.dotCNS.chat + BDFDB.dotCN.chatcontent, {name:"chatContentObserver",instance:observer}, {childList:true}); observer = new MutationObserver((changes, _) => { changes.forEach( (change, i) => { if (change.addedNodes) { change.addedNodes.forEach((node) => { let pagination = null; if (node && node.tagName && (pagination = node.querySelector(BDFDB.dotCN.searchresultspagination)) != null) { this.addNewControls(pagination); } }); } } ); }); BDFDB.addObserver(this, BDFDB.dotCNS.searchresultswrap, {name:"searchResultsObserver",instance:observer}, {childList:true, subtree:true}); let pagination = document.querySelector(BDFDB.dotCNS.searchresultswrap + BDFDB.dotCNS.searchresultspagination); if (pagination) this.addNewControls(pagination); return true; } else { console.error(this.name + ": Fatal Error: Could not load BD functions!"); return false; } } onStop () { if (typeof BDFDB === "object") { document.querySelectorAll(".BSP-pagination, .BSP-pagination-button, .BSP-pagination-jumpinput").forEach(ele => {ele.remove();}); BDFDB.unloadMessage(this); return true; } else { return false; } } onSwitch () { BDFDB.addObserver(this, BDFDB.dotCNS.chat + BDFDB.dotCN.chatcontent, {name:"chatContentObserver"}, {childList:true}); BDFDB.addObserver(this, BDFDB.dotCNS.searchresultswrap, {name:"searchResultsObserver"}, {childList:true, subtree:true}); let pagination = document.querySelector(BDFDB.dotCNS.searchresultswrap + BDFDB.dotCNS.searchresultspagination); if (pagination) this.addNewControls(pagination); } // begin of own functions updateSettings (settingspanel) { var settings = {}; for (var input of settingspanel.querySelectorAll(BDFDB.dotCN.switchinner)) { settings[input.value] = input.checked; } BDFDB.saveAllData(settings, this, "settings"); } addNewControls (pagination) { if (!pagination || document.querySelector(".BSP-pagination, .BSP-pagination-button, .BSP-pagination-jumpinput")) return; let searchResults = document.querySelector(BDFDB.dotCN.searchresults); let searchID = this.SearchUtils.getCurrentSearchId(); if (!searchResults || !searchID) return; let currentpage, maxpage; for (let word of pagination.textContent.split(" ")) { let number = parseInt(word.replace(/\./g,"")); if (!isNaN(number) && !currentpage) { currentpage = number; } else if (!isNaN(number)) { maxpage = number; break; } } if (!currentpage || !maxpage) return; let temppage = currentpage; currentpage = currentpage < maxpage ? currentpage : maxpage; maxpage = temppage < maxpage ? maxpage : temppage; if (maxpage > 201) { if (currentpage == 201) BDFDB.showToast("Discord doesn't allow you to go further than page 201.",{type:"error"}); maxpage = 201; } if (currentpage == maxpage && maxpage == 201) pagination.querySelector(BDFDB.dotCN.searchresultspaginationnext).classList.add(BDFDB.disCN.searchresultspaginationdisabled); let settings = BDFDB.getAllData(this, "settings"); if (settings.addFirstLast) { let BSPpaginatonFirst = document.createElement("div"); BSPpaginatonFirst.className = "BSP-pagination-button BSP-pagination-first"; if (currentpage == 1) BSPpaginatonFirst.classList.add(BDFDB.disCN.searchresultspaginationdisabled); pagination.insertBefore(BSPpaginatonFirst, pagination.firstElementChild); let BSPpaginatonLast = document.createElement("div"); BSPpaginatonLast.className = "BSP-pagination-button BSP-pagination-last"; if (currentpage == maxpage) BSPpaginatonLast.classList.add(BDFDB.disCN.searchresultspaginationdisabled); pagination.appendChild(BSPpaginatonLast); } if (settings.addJumpTo) { let jumpInput = $(`
`)[0]; pagination.appendChild(jumpInput); } BDFDB.initElements(pagination); if (settings.cloneToTheTop) { let BSPpaginaton = pagination.cloneNode(true); BSPpaginaton.classList.add("BSP-pagination"); searchResults.parentElement.insertBefore(BSPpaginaton, searchResults); BDFDB.initElements(BSPpaginaton); } $(searchResults.parentElement) .off("click." + this.name).off("keyup." + this.name) .on("click." + this.name, BDFDB.dotCN.searchresultspaginationprevious + BDFDB.dotCN.searchresultspaginationdisabled, (e) => { e.preventDefault(); e.stopPropagation(); }) .on("click." + this.name, BDFDB.dotCN.searchresultspaginationnext + BDFDB.dotCN.searchresultspaginationdisabled, (e) => { e.preventDefault(); e.stopPropagation(); }) .on("click." + this.name, ".BSP-pagination " + BDFDB.dotCN.searchresultspaginationprevious + ":not(" + BDFDB.dotCN.searchresultspaginationdisabled + ")", () => { this.SearchNavigation.searchPreviousPage(searchID); }) .on("click." + this.name, ".BSP-pagination " + BDFDB.dotCN.searchresultspaginationnext + ":not(" + BDFDB.dotCN.searchresultspaginationdisabled + ")", () => { this.SearchNavigation.searchNextPage(searchID); }) .on("click." + this.name, ".BSP-pagination-first:not(" + BDFDB.dotCN.searchresultspaginationdisabled + ")", () => { for (let i = 0; currentpage - 1 - i > 0; i++) { this.SearchNavigation.searchPreviousPage(searchID); } }) .on("click." + this.name, ".BSP-pagination-last:not(" + BDFDB.dotCN.searchresultspaginationdisabled + ")", () => { for (let i = 0; maxpage - currentpage - i > 0; i++) { this.SearchNavigation.searchNextPage(searchID); } }) .on("keyup." + this.name, ".BSP-pagination-jumpinput " + BDFDB.dotCN.inputmini, (e) => { if (e.which == 13) { let value = e.target.value; if (value < 1 || value > maxpage) { e.target.value = currentpage; if (maxpage == 201 && value > maxpage) BDFDB.showToast("Discord doesn't allow you to go further than page 201.",{type:"error"}); } else if (value < currentpage) { for (; currentpage - value > 0; value++) { this.SearchNavigation.searchPreviousPage(searchID); } } else if (value > currentpage) { for (; value - currentpage > 0; value--) { this.SearchNavigation.searchNextPage(searchID); } } } }); } getSettingsPanel () { var settings = BDFDB.getAllData(this, "settings"); var settingshtml = `
`; for (let key in settings) { settingshtml += `

${this.defaults.settings[key].description}

`; } settingshtml += `
`; var settingspanel = $(settingshtml)[0]; $(settingspanel) .on("click", BDFDB.dotCN.switchinner, () => {this.updateSettings(settingspanel);}); return settingspanel; } onSettingsClosed () { document.querySelectorAll(".BSP-pagination, .BSP-pagination-button, .BSP-pagination-jumpinput").forEach(ele => {ele.remove();}); let pagination = document.querySelector(BDFDB.dotCNS.searchresultswrap + BDFDB.dotCNS.searchresultspagination); if (pagination) this.addNewControls(pagination); } } };