deep-rock-wiki

Transform https://deeprockgalactic.wiki.gg pages into https://deeprock.wiki link

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         deep-rock-wiki
// @namespace    https://github.com/imDaniX
// @version      1.2.0
// @description  Transform https://deeprockgalactic.wiki.gg pages into https://deeprock.wiki link
// @author       imDaniX
// @homepageURL  https://github.com/imDaniX/deep-rock-wiki
// @license      MIT; https://opensource.org/license/mit
// @match        https://deeprockgalactic.wiki.gg/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=deeprockgalactic.wiki.gg
// ==/UserScript==

const MAIN_DOMAIN = "deeprock.wiki"
const MAIN_PAGE = "/Deep_Rock_Galactic_Wiki";
const SURVIVOR_NAMESPACE = "/Survivor:"
const SURVIVOR_PAGE = SURVIVOR_NAMESPACE + "Main";
const RC_NAMESPACE = "/Rogue_Core:"
const RC_PAGE = RC_NAMESPACE + "Main";

(function() {
    'use strict';

    addEventListener("copy", (event) => {
        let location = window.location;
        let query = location.search;
        if (hasSelection() || query.includes("action=edit") || query.includes("action=submit")) return;

        let path = location.pathname.replace("index.php", "").replace("/wiki", "");
        let domain = MAIN_DOMAIN;
        if (path == MAIN_PAGE || path == "/") {
            path = "";
        } else if (path.startsWith(SURVIVOR_NAMESPACE)) {
            domain = "survivor." + domain;
            path = path != SURVIVOR_PAGE
                ? path.replace("Survivor:", "")
                : ""
        } else if (path.startsWith(RC_NAMESPACE)) {
            domain = "rc." + domain;
            path = path != RC_PAGE
                ? path.replace("Rogue_Core:", "")
                : ""
        }

        path = decodeURI(path + query + location.hash).replaceAll(" ", "%20");

        event.clipboardData.setData("text/plain", `https://${domain}${path}`);
        event.preventDefault();
    });
})();

/**
 * https://stackoverflow.com/a/5379408
 */
function hasSelection() {
    let text = "";
    const activeEl = document.activeElement;
    const activeElTagName = activeEl ? activeEl.tagName.toLowerCase() : null;

    if (
      (activeElTagName == "textarea") || (activeElTagName == "input" && /^(?:text|search|password|tel|url)$/i.test(activeEl.type)) &&
      (typeof activeEl.selectionStart == "number")
    ) {
        text = activeEl.value.slice(activeEl.selectionStart, activeEl.selectionEnd);
    } else if (window.getSelection) {
        text = window.getSelection().toString();
    }

    return text && text != "";
}