Gist to dabblet

Add a dabblet.com link button to any gist with dabblet information

As of 2019-02-17. See the latest version.

  1. // ==UserScript==
  2. // @name Gist to dabblet
  3. // @version 2.1.5
  4. // @description Add a dabblet.com link button to any gist with dabblet information
  5. // @license MIT
  6. // @author Rob Garrison
  7. // @namespace https://github.com/Mottie
  8. // @include https://gist.github.com/*
  9. // @run-at document-idle
  10. // @grant none
  11. // @icon https://github.githubassets.com/pinned-octocat.svg
  12. // ==/UserScript==
  13. (() => {
  14. "use strict";
  15.  
  16. const content = `
  17. <a href="http://dabblet.com/gist/{gistid}" class="{class} tooltipped tooltipped-s" aria-label="Open at Dabblet.com">
  18. <svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" width="16" height="10" viewBox="0 0 16 10">
  19. <path d="M1.42 6.64c0-.46.1-.88.27-1.3.16-.4.4-.74.7-1.04.3-.3.65-.54 1.05-.7.4-.18.84-.27 1.3-.27.24 0 .48.03.7.08.23.06.45.14.66.23V1.2c0-.15.04-.28.1-.4s.1-.2.2-.3.2-.16.3-.2.25-.1.4-.1c.13 0 .25.04.37.1.12.04.22.1.3.2.1.1.17.2.22.3s.06.25.06.4V6.66c0 .3-.04.6-.12.87s-.2.55-.34.8-.32.46-.52.65c-.2.2-.43.36-.67.5s-.5.26-.78.34c-.28.07-.58.1-.88.1-.3 0-.6-.03-.88-.1s-.54-.2-.8-.34c-.23-.15-.46-.32-.66-.52s-.38-.42-.52-.67c-.15-.23-.26-.5-.34-.78-.08-.28-.12-.58-.12-.88zm1.96 0c0 .2.03.36.1.53s.17.3.3.43c.12.13.27.23.43.3.18.07.36.1.54.1.2 0 .37-.03.53-.1.17-.07.3-.17.44-.3.13-.12.23-.27.3-.43s.1-.34.1-.53c0-.2-.03-.36-.1-.53-.07-.15-.17-.3-.3-.42-.12-.13-.26-.22-.43-.3-.16-.07-.34-.1-.53-.1-.18 0-.36.03-.53.1-.15.08-.3.17-.42.3-.13.12-.22.27-.3.43-.07.18-.1.35-.1.54zM12.6 9.97c-.3 0-.6-.04-.88-.12s-.55-.2-.8-.34c-.24-.13-.47-.3-.67-.5-.2-.2-.37-.43-.52-.68-.14-.24-.26-.5-.33-.8s-.12-.56-.12-.87V1c0-.13.02-.26.07-.37.06-.12.13-.22.2-.3.1-.1.2-.17.32-.22.12-.04.25-.07.38-.07.14 0 .27.03.4.08.1.06.2.13.3.22.08.1.15.2.2.3.06.13.08.25.08.4v5.63c0 .2.04.37.1.53s.18.3.3.44c.13.12.27.22.44.3.16.06.34.1.53.1.13 0 .26.02.38.07s.22.12.3.2c.1.1.16.2.22.3s.07.26.07.4c0 .13-.02.26-.07.37-.06.12-.13.22-.2.3-.1.1-.2.17-.32.22s-.25.07-.38.07zm.42-6.22c0 .13-.03.25-.08.36-.05.12-.12.22-.2.3-.1.1-.2.15-.3.2-.12.05-.24.07-.37.07s-.25-.03-.36-.08c-.1-.06-.2-.13-.3-.22-.07-.08-.14-.18-.2-.3s-.05-.23-.05-.36.03-.25.08-.36.12-.22.2-.3c.1-.08.2-.15.3-.2s.24-.07.37-.06c.13 0 .25.03.36.08.1.05.2.12.3.2.08.1.15.2.2.3.04.12.06.24.06.37z"/>
  20. </svg>
  21. dabblet
  22. </a>`;
  23.  
  24. function findDabbletGist() {
  25. let indx, el, button;
  26. const list = [],
  27. // main gist page
  28. gist = $("#file-dabblet-css"),
  29. // list of gists page
  30. lists = $$(".css-truncate-target");
  31.  
  32. if ($$(".gist-snippet").length) {
  33. indx = lists.length;
  34. while (indx--) {
  35. // only save dabblet files from list
  36. if (lists[indx].textContent.indexOf("dabblet.css") > -1) {
  37. list[list.length] = lists[indx];
  38. }
  39. }
  40. }
  41. const len = list.length;
  42. if (gist || len) {
  43. if (len) {
  44. for (indx = 0; indx < len; indx++) {
  45. button = document.createElement("li");
  46. button.innerHTML = content
  47. .replace("{gistid}", list[indx].parentNode.href.match(/[a-f\d]+$/))
  48. .replace("{class}", "");
  49. el = $(".gist-count-links li", closest(".gist-snippet-meta", list[indx]));
  50. el.parentNode.insertBefore(button, el);
  51. el.parentNode.style.zIndex = 1;
  52. }
  53. } else if (gist) {
  54. button = document.createElement("li");
  55. button.innerHTML = content
  56. .replace("{gistid}", window.location.pathname.match(/[a-f\d]+$/))
  57. .replace("{class}", "btn btn-sm");
  58. el = $(".pagehead-actions li");
  59. el.parentNode.insertBefore(button, el);
  60. }
  61. }
  62. }
  63.  
  64. function $(str, el) {
  65. return (el || document).querySelector(str);
  66. }
  67.  
  68. function $$(str, el) {
  69. return Array.from((el || document).querySelectorAll(str));
  70. }
  71.  
  72. function closest(selector, el) {
  73. while (el && el.nodeType === 1) {
  74. if (el.matches(selector)) {
  75. return el;
  76. }
  77. el = el.parentNode;
  78. }
  79. return null;
  80. }
  81.  
  82. document.addEventListener("pjax:end", findDabbletGist);
  83. findDabbletGist();
  84. })();