Amazon Gog Redeemer

Provide a link to Gog.com from Amazon Prime Games

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Amazon Gog Redeemer
// @namespace    FaustVXAmazonGog
// @version      1.1
// @description  Provide a link to Gog.com from Amazon Prime Games
// @author       FaustVX
// @match        https://gaming.amazon.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=gaming.amazon.com
// @grant        none
// @contributionURL https://www.paypal.com/donate/?cmd=_donations&[email protected]&item_name=TamperMonkey+Amazon+Gog+Redeemer
// @license         MIT
// @supportURL      https://gist.github.com/FaustVX/480c9fb00d71d50bb2faff16524e2d31#comments
// ==/UserScript==

function run() {
    'use strict';

    const urlSplit = window.location.href.split('/');
    const domainName = urlSplit[2].split('.');

    function changeHref(query) {
        return function (a) {
            const urlParams = new URLSearchParams(a.search);
            const url = urlParams.get(query);
            a.href = decodeURIComponent(url);
        }
    }

    function changeTag(node, tag) {
        const clone = createElement(tag)
        for (const attr of node.attributes) {
            clone.setAttributeNS(null, attr.name, attr.value)
        }
        while (node.firstChild) {
            clone.appendChild(node.firstChild)
        }
        node.replaceWith(clone)
        return clone
    }

    function createElement(tag) {
        return document.createElementNS(tag === 'svg' ? 'http://www.w3.org/2000/svg' : 'http://www.w3.org/1999/xhtml', tag)
    }

    function execute(isActivated, selectorAll, foreach) {
        if (isActivated) {
            document.querySelectorAll(selectorAll).forEach(foreach);
            return true;
        }
        return false;
    }

    execute(domainName[1] === "amazon" && !urlSplit[3].endsWith("microsoft"), 'button[data-a-target="copy-code"] span[data-a-target="tw-button-text"]:not(:has(a))', function(s) {
        const href = "https://www.gog.com/redeem/" + s.parentNode.parentNode.parentNode.parentNode.previousSibling.lastChild.lastChild.attributes.value.textContent;
        const a = createElement('a');
        a.href = href;
        a.innerHTML = "Redeem on Gog";
        a.style="color:white";
        a.target="_blank";
        s.replaceWith(a)
    })
};

function runWhenReady(callback) {
    const tryNow = function() {
        try {
            callback();
        } catch { }
        setTimeout(tryNow, 250);
    };
    tryNow();
}

runWhenReady(run);