// ==UserScript== // @name Gist to dabblet // @version 2.1.5 // @description Add a dabblet.com link button to any gist with dabblet information // @license MIT // @author Rob Garrison // @namespace https://github.com/Mottie // @match https://gist.github.com/* // @run-at document-idle // @grant none // @icon https://github.githubassets.com/pinned-octocat.svg // @updateURL https://raw.githubusercontent.com/Mottie/GitHub-userscripts/master/gist-to-dabblet.user.js // @downloadURL https://raw.githubusercontent.com/Mottie/GitHub-userscripts/master/gist-to-dabblet.user.js // @supportURL https://github.com/Mottie/GitHub-userscripts/issues // ==/UserScript== (() => { "use strict"; const content = ` dabblet `; function findDabbletGist() { let indx, el, button; const list = [], // main gist page gist = $("#file-dabblet-css"), // list of gists page lists = $$(".css-truncate-target"); if ($$(".gist-snippet").length) { indx = lists.length; while (indx--) { // only save dabblet files from list if (lists[indx].textContent.indexOf("dabblet.css") > -1) { list[list.length] = lists[indx]; } } } const len = list.length; if (gist || len) { if (len) { for (indx = 0; indx < len; indx++) { button = document.createElement("li"); button.innerHTML = content .replace("{gistid}", list[indx].parentNode.href.match(/[a-f\d]+$/)) .replace("{class}", ""); el = $(".gist-count-links li", closest(".gist-snippet-meta", list[indx])); el.parentNode.insertBefore(button, el); el.parentNode.style.zIndex = 1; } } else if (gist) { button = document.createElement("li"); button.innerHTML = content .replace("{gistid}", window.location.pathname.match(/[a-f\d]+$/)) .replace("{class}", "btn btn-sm"); el = $(".pagehead-actions li"); el.parentNode.insertBefore(button, el); } } } function $(str, el) { return (el || document).querySelector(str); } function $$(str, el) { return Array.from((el || document).querySelectorAll(str)); } function closest(selector, el) { while (el && el.nodeType === 1) { if (el.matches(selector)) { return el; } el = el.parentNode; } return null; } document.addEventListener("pjax:end", findDabbletGist); findDabbletGist(); })();