IsThereAnyDeal link on EpicGames Store

Puts an IsThereAnyDeal link to the game pages on Epic Games Store

目前為 2020-11-23 提交的版本,檢視 最新版本

// ==UserScript==
// @name         IsThereAnyDeal link on EpicGames Store
// @namespace    1N07
// @version      0.2
// @description  Puts an IsThereAnyDeal link to the game pages on Epic Games Store
// @author       1N07
// @include      https://isthereanydeal.com/search/?q=*
// @include      https://www.epicgames.com/*
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @grant        GM_getValue
// @grant        GM_setValue
// @noframes
// ==/UserScript==

(function() {
    'use strict';

    var AutoReDirectToProbableMatchHandle;
    var AutoReDirectToProbableMatch = GM_getValue("AutoReDirectToProbableMatch", true);
    SetAutoReDirectToProbableMatchHandle();

    //IsThereAnyDeal
    if(AutoReDirectToProbableMatch && window.location.href.match(/https:\/\/isthereanydeal.com\/search\/\?q=.+?\&redirect=1/g))
    {
        var reDirInterval, reDirLinks;
        reDirInterval = setInterval(function(){
            reDirLinks = document.querySelectorAll("#pageContainer .card__title");
            if(reDirLinks !== null && reDirLinks.length > 0)
            {
                let searchTerm = window.location.href.split("q=")[1].split("&redirect=1")[0];
                let searchTermRE = new RegExp(searchTerm,"g");
                clearInterval(reDirInterval);
                let leastExtraEl = null, leastExtraNum = 99999;
                for(let i = 0; i < reDirLinks.length; i++)
                {
                    let thisLen = reDirLinks[i].textContent.replace(searchTermRE, "").length;
                    if(thisLen < leastExtraNum)
                    {
                        leastExtraEl = reDirLinks[i];
                        leastExtraNum = thisLen;
                    }
                }
                if(leastExtraEl !== null)
                {
                    try {leastExtraEl.parentNode.parentNode.parentNode.parentNode.style.border = "5px dotted greenyellow";} catch(err){}
                    window.location.href = leastExtraEl.href;
                }
            }
        }, 200);
    }
    else
    {
        var lastUrl = null;
        setInterval(() => {
            if(window.location.href != lastUrl)
                OnUrlChange();
        }, 200);
    }

    function OnUrlChange()
    {
        lastUrl = window.location.href;

        //Epic Game Store Page
        if(window.location.href.match(/https:\/\/www\.epicgames\.com\/store\/.+?\/product\/.+/g))
        {
            var insertInterval, place;
            insertInterval = setInterval(function(){
                place = document.querySelector('[data-component="ProductDetailHeader"] div[class*="ctaInner"]');
                if(place !== null)
                {
                    clearInterval(insertInterval);

                    addGlobalStyle(`
						#ITADButt {
							font-size: 11px;
							letter-spacing: 0.5px;
							font-weight: 500;
							display: block;
							position: relative;
							border-radius: 4px;
							text-transform: none;
							text-align: center;
							-moz-box-align: center;
							align-items: center;
							-moz-box-pack: center;
							justify-content: center;
							line-height: 15px;
							padding: 5px 20px;
							margin: 5px auto;
							background-color: royalblue;
							color: rgb(245, 245, 245);
							height: 35px;
							width: auto;
							min-width: auto;
						}

						#ITADButt:hover, #ITADButt:focus, #ITADButt:active
						{
							filter: brightness(125%);
						}
					`);

                    let newElem = CreateHTMLFrag(`<button id="ITADButt" style="background-color: royalblue;height: 35px;padding: 5px 20px;margin: 5px auto;display: block;"><span class="css-hahhpe-PurchaseCTA__ctaText">IsThereAnyDeal</span></button>`);
                    place.appendChild(newElem);
                    document.getElementById("ITADButt").onclick = function(){GoToITAD();};
                }
            }, 200);
        }
    }
    function GoToITAD()
    {
        let name = document.getElementById("page-meta-keywords").getAttribute("content"); //this seems to be a fairly reliable way to get the title of the game without any affixes or what have you, like editions etc.
        if(name === null || name.length == 0)
            name = document.querySelector('li[data-component="NavigationList"] h2 > span').textContent; //if it's missing from head (noticed at least one case), get it from the top of the page

        let link = "https://isthereanydeal.com/search/?q=" + encodeURIComponent(name) + (AutoReDirectToProbableMatch ? "&redirect=1" : "");
        window.open(link, "_blank");
    }

    function CreateHTMLFrag(htmlStr) {
        var frag = document.createDocumentFragment(),
            temp = document.createElement('div');
        temp.innerHTML = htmlStr;
        while (temp.firstChild) {
            frag.appendChild(temp.firstChild);
        }
        return frag;
    }
    function addGlobalStyle(css)
    {
        var head, style;
        head = document.getElementsByTagName('head')[0];
        if (!head) { return; }
        style = document.createElement('style');
        style.type = 'text/css';
        style.innerHTML = css;
        head.appendChild(style);
    }
    function SetAutoReDirectToProbableMatchHandle() {
        GM_unregisterMenuCommand(AutoReDirectToProbableMatchHandle);

        AutoReDirectToProbableMatchHandle = GM_registerMenuCommand("Auto-redirect to probable match (" + (AutoReDirectToProbableMatch ? "On" : "Off") + ") -click to change-", function(){
            AutoReDirectToProbableMatch = !AutoReDirectToProbableMatch;
            GM_setValue("AutoReDirectToProbableMatch", AutoReDirectToProbableMatch);
            SetAutoReDirectToProbableMatchHandle();

            if(confirm('Press "OK" to refresh the page to apply new settings'))
                location.reload();
        });
    }
})();