Open As User Script

Opens gitlab or github files as a userscript on Violentmonkey

Verze ze dne 28. 08. 2024. Zobrazit nejnovější verzi.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Open As User Script
// @namespace    https://gitlab.com/Dwyriel
// @version      0.3.1
// @description  Opens gitlab or github files as a userscript on Violentmonkey
// @author       Dwyriel
// @license      MIT
// @match        *://*gitlab.com/*
// @match        *://*github.com/*
// @grant        none
// @run-at       document-idle
// @homepageURL  https://gitlab.com/Dwyriel/Greasyfork-Scripts
// ==/UserScript==

(function () {
    'use strict';
    const imgData = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKAAAACgCAYAAACLz2ctAAAAAXNSR0IArs4c6QAAEVVJREFUeF7tXXnUftUUfh5TIqKRRINmSYVQhlaRoUGy0qBRtJBKKgkRRS0pyZClDM1qZYyW1EKRDC1DaaBEmsiUIWXIdp863+rr/b3ve/c99573vt/37b3WXd8f7z77nPOc57v3DPvsTYQEAj0iwB7rjqoDAQQBgwS9IhAE7BX+qDwIGBzoFYEgYK/wR+VBwOBArwgEAXuFPyoPAgYHekUgCNgr/FF5EDA40CsCQcBe4Y/Kg4DBgV4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WomPk7KtO8PK5vB6AtHaWsvbbKdHRl9fb9UZcnGW3hCQK2RbDn8smHT4RUSA152Wjr5K6uU2qV6mYQsBSyYdeFQBDQBVMolUIgCFgK2bDrQiAI6IIplEohEAQshWzYdSEQBHTBFEqlEAgClkI27LoQCAK6YAqlUggEAUshG3ZdCAQBXTCFUikEgoClkA27LgSCgC6YQqkUAkHAUsiGXRcCQUAXTKFUCoEgYClkw64LgSCgC6ZQKoVAELAUsmHXhUAQ0AVTKJVC4P/WcxT7P5jCKQAAAABJRU5ErkJggg==";
    const elementID = "OaUS_g893n1g7f561nf";
    const config = { attributes: true, childList: true, subtree: true };
    function openUrl(url) {
        var newWindow = window.open(url, "File as UserScript");
        let intervalHandler = setInterval(() => {
            if (newWindow.closed) {
                clearInterval(intervalHandler);
                return;
            }
            newWindow.close();
        }, 1000);
    }
    function genUrlFromGitlab() {
        let url = window.location.toString().replace('blob', 'raw');
        return url.includes("?") ? `${url}&file=as/script.user.js` : `${url}?file=as/script.user.js`;
    }
    function genUrlFromGithub() {
        let url = window.location.toString().replace('github.com', 'raw.githubusercontent.com').replace('blob/', '');
        return url.includes("?") ? `${url}&file=as/script.user.js` : `${url}?file=as/script.user.js`;
    }
    function gitlabCallback() {
        if (document.getElementById(elementID))
            return;
        let btnToClone = document.querySelector(`div[data-testid="default-actions-container"]`)?.querySelector("[title*='raw']");
        if (!btnToClone)
            return;
        let clonedNode = btnToClone.cloneNode(true);
        let img = document.createElement("img");
        let svg = clonedNode.children[0];
        img.classList = svg.classList;
        img.setAttribute('src', imgData);
        svg.after(img);
        svg.remove();
        clonedNode.id = elementID;
        clonedNode.removeAttribute('href');
        clonedNode.setAttribute('aria-label', "Open as Userscript");
        clonedNode.setAttribute('title', "Open as Userscript");
        clonedNode.onclick = function () { openUrl(genUrlFromGitlab()) };
        btnToClone.after(clonedNode);
    }
    function githubCallbcak() {
        if (document.getElementById(elementID))
            return;
        let btnToClone = document.querySelectorAll('[class*="ButtonGroup"]')[0]?.parentNode.querySelector('[data-testid*="copy-raw"]');
        if (!btnToClone)
            return;
        let clonedNode = btnToClone.cloneNode(true);
        let img = document.createElement("img");
        let svg = clonedNode.children[0];
        img.classList = svg.classList;
        img.setAttribute('src', imgData);
        img.setAttribute('width', 16);
        img.setAttribute('height', 16);
        svg.after(img);
        svg.remove();
        clonedNode.id = elementID;
        clonedNode.removeAttribute('data-hotkey');
        clonedNode.removeAttribute('aria-describeby');
        clonedNode.setAttribute('aria-label', "Open as Userscript");
        clonedNode.setAttribute('title', "Open as Userscript");
        clonedNode.setAttribute('data-testid', "open-as-userscript");
        clonedNode.onclick = function () { openUrl(genUrlFromGithub()) };
        btnToClone.after(clonedNode);
    }
    if (window.location.toString().includes("//gitlab.com/"))
        new MutationObserver(gitlabCallback).observe(document.body, config);
    else
        new MutationObserver(githubCallbcak).observe(document.body, config);
})();