Gist to dabblet

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

目前为 2016-03-29 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Gist to dabblet
  3. // @version 2.0.0
  4. // @description Add a dabblet.com link button to any gist with dabblet information
  5. // @license https://creativecommons.org/licenses/by-sa/4.0/
  6. // @namespace http://github.com/Mottie
  7. // @include https://gist.github.com/*
  8. // @grant GM_addStyle
  9. // @run-at document-idle
  10. // @author Rob Garrison
  11. // @icon64URL http://mottie.github.io/gist-to-dabblet/images/g2d.png
  12. // ==/UserScript==
  13. (function() {
  14. "use strict";
  15.  
  16. GM_addStyle(".g2d-button { display:inline-block; width:18px; background-repeat:no-repeat; background-position:center top; }");
  17.  
  18. var button,
  19.  
  20. icons = {
  21. grey : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQBAMAAADt3eJSAAAALVBMVEUAAABVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVX///9VVVX37ue8AAAADnRSTlMARN1VIjMR7sy7ZneqEdMqVloAAABNSURBVAjXYyASOKk5qYEZcg/lHmJlWLzTeyj36DED97t374CMVwx+L1n0wFLrnjLYwRlP5R4zgKUU0l8kgBULML97znDB4t1RBgbJ5wDtZybkJeFKhwAAAABJRU5ErkJggg==",
  22. white : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQBAMAAADt3eJSAAAAKlBMVEUAAAD///////////////////////////////////////////////////+Gu8ovAAAADXRSTlMAd+7dRKqZiGbMIhEzr1+TpQAAAExJREFUCNdjIBL4BvgGgBmyF2QvYGUI3bW9IHtZi4H17t27QIYOQ+9FlrtgqVwFhlo4Q0H2NgNYKsH3GgNI8eUF7HdvgrRXMzA03wQARXIiwwVnjbIAAAAASUVORK5CYII=",
  23. black : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQBAMAAADt3eJSAAAAJ1BMVEUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADdEvm1AAAADHRSTlMAu3fdzBGqmYhmRO5Nxwc9AAAATElEQVQI12MgEugo6CiAGTICMgLYGKw9Z2wEZGwcGILPnDkDZLgy1JxiOgOWsklg8IEzEmQcGMBSE3ROMoAUH2RgOXMCqP20AtCW4wBomxaqzmf3OgAAAABJRU5ErkJggg=="
  24. },
  25.  
  26. content = [
  27. "<a href='http://dabblet.com/gist/{gistid}' class='{class} tooltipped tooltipped-n' aria-label='Open at Dabblet.com'>",
  28. "<span class='g2d-button' style='background-image:url({icon})'>&nbsp;</span>",
  29. "&nbsp;dabblet",
  30. "</a>"
  31. ].join(""),
  32.  
  33. hasClass = function(el, name) {
  34. if (el) {
  35. return el.classList ?
  36. el.classList.contains(name) :
  37. new RegExp("\\b" + name + "\\b").test(el.className);
  38. }
  39. return false;
  40. },
  41.  
  42. closest = function(el, name) {
  43. while (el && !hasClass(el, name)) {
  44. el = el.parentNode;
  45. }
  46. return hasClass(el, name) ? el : null;
  47. },
  48.  
  49. findDabbletGist = function() {
  50. var indx, len, el,
  51. list = [],
  52. hasDabblet = false,
  53. // main gist page
  54. gist = document.querySelector("#file-dabblet-css"),
  55. // list of gists page
  56. lists = document.querySelectorAll(".css-truncate-target");
  57.  
  58. if (document.querySelectorAll(".gist-snippet").length) {
  59. indx = lists.length;
  60. while (indx--) {
  61. // only save dabblet files from list
  62. if (lists[indx].textContent.indexOf("dabblet.css") > -1) {
  63. list[list.length] = lists[indx];
  64. }
  65. }
  66. }
  67. len = list.length;
  68. if (gist || len) {
  69. if (len) {
  70. for (indx = 0; indx < len; indx++) {
  71. button = document.createElement("li");
  72. button.innerHTML = content
  73. .replace("{gistid}", list[indx].parentNode.href.match(/\d+$/))
  74. .replace("{class}", "")
  75. .replace("{icon}", icons.grey);
  76. el = closest(list[indx], "gist-snippet-meta").querySelector(".gist-count-links li");
  77. el.parentNode.insertBefore(button, el);
  78. el.parentNode.style.zIndex = 1;
  79. }
  80. } else if (gist) {
  81. button = document.createElement("li");
  82. button.innerHTML = content
  83. .replace("{gistid}", window.location.pathname.match(/\d+$/))
  84. .replace("{class}", "btn btn-sm")
  85. .replace("{icon}", icons.black);
  86. el = document.querySelector(".pagehead-actions li");
  87. el.parentNode.insertBefore(button, el);
  88. }
  89. }
  90. },
  91.  
  92. targets = document.querySelectorAll("#js-repo-pjax-container, #js-pjax-container, .js-preview-body");
  93.  
  94. Array.prototype.forEach.call(targets, function(target) {
  95. new MutationObserver(function(mutations) {
  96. mutations.forEach(function(mutation) {
  97. // preform checks before adding code wrap to minimize function calls
  98. if (!busy && mutation.target === target) {
  99. findDabbletGist();
  100. }
  101. });
  102. }).observe(target, {
  103. childList: true,
  104. subtree: true
  105. });
  106. });
  107.  
  108. findDabbletGist();
  109.  
  110. })();