// ==UserScript== // @name Open As User Script // @namespace https://gitlab.com/Dwyriel // @version 0.4.0 // @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 // @downloadURL https://update.greasyfork.icu/scripts/505561/Open%20As%20User%20Script.user.js // @updateURL https://update.greasyfork.icu/scripts/505561/Open%20As%20User%20Script.meta.js // ==/UserScript== (function () { 'use strict'; const svgHTML = ``; const elementID_1 = "OaUS_g893n1g7f561nf_1"; const elementID_2 = "OaUS_g893n1g7f561nf_2"; const config = { attributes: true, childList: true, subtree: true }; const svgDoc = new DOMParser().parseFromString(svgHTML, 'image/svg+xml'); let mutationObs = null; 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 gitlabCallback() { if (document.getElementById(elementID_1)) return; let btnToClone = document.querySelector(`div[data-testid="default-actions-container"]`)?.querySelector("[title*='raw']"); if (!btnToClone) return; let clonedNode = btnToClone.cloneNode(true); let clonedSVG = svgDoc.firstChild.cloneNode(true); let svg = clonedNode.children[0]; clonedSVG.classList = svg.classList; svg.after(clonedSVG); svg.remove(); clonedNode.id = elementID_1; 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 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 githubCreateDesktopButton() { if (document.getElementById(elementID_1)) return; let btnToClone = document.querySelectorAll('[class*="ButtonGroup"]')[0]?.parentNode.querySelector('[data-testid*="copy-raw"]'); if (!btnToClone) return; let clonedNode = btnToClone.cloneNode(true); let clonedSVG = svgDoc.firstChild.cloneNode(true); let svg = clonedNode.children[0]; clonedSVG.classList = svg.classList; clonedSVG.setAttribute('width', 16); clonedSVG.setAttribute('height', 16); svg.after(clonedSVG); svg.remove(); clonedNode.id = elementID_1; 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); } function githubCreateMobileButton() { if (document.getElementById(elementID_2)) return; let rawFileList = document.querySelectorAll('[class*="List__ListBox"]')[0]?.lastChild; if (!rawFileList) return; let listEntries = rawFileList.querySelectorAll('[class*="Link__StyledLink"]'); if (listEntries.length == 0) return; let listEntry = listEntries[listEntries.length - 1].parentNode; let clonedNode = listEntry.cloneNode(true); clonedNode.id = elementID_2; clonedNode.firstChild.innerText = "As Userscript" clonedNode.firstChild.onclick = function () { openUrl(genUrlFromGithub()) }; listEntry.after(clonedNode); } function githubCallbcak() { if (!document.getElementById(elementID_1)) githubCreateDesktopButton(); if (!document.getElementById(elementID_2)) githubCreateMobileButton(); } if (window.location.toString().includes("//gitlab.com/")) mutationObs = new MutationObserver(gitlabCallback); else if (window.location.toString().includes("//github.com/")) mutationObs = new MutationObserver(githubCallbcak); if (mutationObs) mutationObs.observe(document.body, config); })();