// ==UserScript== // @name Gist to dabblet // @version 2.1.1 // @description Add a dabblet.com link button to any gist with dabblet information // @license https://creativecommons.org/licenses/by-sa/4.0/ // @namespace http://github.com/Mottie // @include https://gist.github.com/* // @run-at document-idle // @author Rob Garrison // @icon64URL http://mottie.github.io/gist-to-dabblet/images/g2d.png // @downloadURL none // ==/UserScript== /* jshint esnext:true, unused:true */ (() => { "use strict"; let busy = false; const content = ` dabblet `; 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; } function findDabbletGist() { busy = true; 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); } } busy = false; } $$("#js-repo-pjax-container, #js-pjax-container, .js-preview-body").forEach(target => { new MutationObserver(mutations => { mutations.forEach(mutation => { // preform checks before adding code wrap to minimize function calls if (!busy && mutation.target === target) { findDabbletGist(); } }); }).observe(target, { childList: true, subtree: true }); }); findDabbletGist(); })();