Grundos Cafe Tab Reducer

Auto closes after shop wizard buy and uses the same tab for SDB searching.

当前为 2024-03-18 提交的版本,查看 最新版本

// ==UserScript==
// @name         Grundos Cafe Tab Reducer
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Auto closes after shop wizard buy and uses the same tab for SDB searching.
// @author       Dij
// @grant        window.close
// @grant        window.focus
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_getTab
// @grant        GM_saveTab
// @grant        GM_addValueChangeListener
// @grant        GM_registerMenuCommand
// @grant        GM_removeValueChangeListener
// @match        https://www.grundos.cafe/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=grundos.cafe
// @license      GPL 3.0
// ==/UserScript==
const re = new RegExp("query=(.+)&"); // grabs the item name from the SDB link
const menu_command_id_1 = GM_registerMenuCommand("Reset stored values", function() {
    alert("Values reset!");
    GM_setValue("sdbTab",0);
    GM_setValue("searchTerm","");
    GM_getTab((tab) => {tab.refresh = 0;
                        GM.saveTab(tab)});
}, {
    autoClose: true
});

function sdbHandler(zEvent) {
    /*If a tab with SDB isn't open already, open one.*/
    if (!GM_getValue("sdbTab")) {
        window.open(`https://www.grundos.cafe/safetydeposit/?page=1&query=${this.searchTerm}&exact=1`);
        GM_setValue("sdbTab",1);
        /*Remove listener and flag on tab close. WARNING: This does not fire if you navigate away
          from SDB on that tab.*/
        window.addEventListener("beforeunload", () => {GM_setValue("sdbTab", 0);
                                                       GM_removeValueChangeListener("searchTerm")});
    } else {
        GM_setValue("searchTerm", this.searchTerm);
    }

}
function sdbSearch(key, oldValue, newValue, remote) {
    window.location.href=`https://www.grundos.cafe/safetydeposit/?page=1&query=${newValue}&exact=1`;
    window.focus();
}

async function sleep(ms) {
    /*i stole this from https://dev.to/noamsauerutley/getting-sleep-with-promises-in-js-5f09
      bc regular setTimeout was just Not working for some reason and im too tired*/
    return new Promise(resolve => setTimeout(resolve, ms));
}

async function setRefreshNo (){
    GM_getTab((tab) => {tab.refresh = 1;
                        GM.saveTab(tab)});
}

async function closeAfterBuy(tab) {
    /*Adds a listener to the searched item, adding a flag persistent to this tab.
      After refreshing, the script can see the flag is set, and closes it after 1 second.*/
    if (tab.refresh){
        console.log("Thank you, you're my favorite customer!");
        tab.refresh = 0;
        await sleep(1000);
        window.close();
    } else {
        var searchedItem = document.querySelector("#shopContent").querySelector("#searchedItem");
        if (searchedItem) {
            searchedItem.tab = tab
            searchedItem.querySelector("form").addEventListener("click", setRefreshNo);
        }
    }
}

function nonSDBHandler(tab){
    if (location.pathname.match(/seshop\/$/)) { // User Shops
        GM_getTab(closeAfterBuy);
    } else {
        var searchhelps = document.querySelector("#page_content").querySelectorAll(".searchhelp");
        for (var i = 0; i < searchhelps.length; i++) {
            /*Captures the search term of each SDB search helper and stores it in the element.
              Replaces the link with my own method that executes on click instead.*/
            var b = searchhelps[i].querySelector('a[href^="/saf"]');
            b.searchTerm = b.href.match(re)[1].replace(/%20/g, " ");
            b.addEventListener("click", sdbHandler);
            b.href = "javascript: void(0)";
            b.target = "";
        }}
}

(function() {
    'use strict';
    if (location.pathname.match(/^\/saf/)) { // SDB
        GM_addValueChangeListener("searchTerm", sdbSearch);
    } else {
        GM_getTab(nonSDBHandler);
    }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址