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.0
// @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
// @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

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);
        window.focus();
        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 (key, oldValue, newValue, remote){
    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() {
    'use strict';
    if (location.pathname.match(/^\/saf/)) { // SDB
        GM_setValue("sdbTab",1); //If you navigate to the SDB not using Helper
        GM_addValueChangeListener("searchTerm", sdbSearch);
    } else if (location.pathname.match(/seshop\/$/)) { // User Shops
        GM_getTab((tab)=>{closeAfterBuy(tab)});
    } 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"]');
            console.log(b);
            b.searchTerm = b.href.match(re)[1].replace(/%20/g, " ");
            b.addEventListener("click", sdbHandler);
            b.href="javascript: void(0)";
            b.target="";
        }
    }
})();

QingJ © 2025

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