GitHub Label Color Picker

A userscript that adds a color picker to the label color input

当前为 2017-03-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Label Color Picker
  3. // @version 1.0.2
  4. // @description A userscript that adds a color picker to the label color input
  5. // @license https://creativecommons.org/licenses/by-sa/4.0/
  6. // @namespace https://github.com/Mottie
  7. // @include https://github.com/*
  8. // @grant GM_addStyle
  9. // @grant GM_getValue
  10. // @grant GM_setValue
  11. // @grant GM_registerMenuCommand
  12. // @require https://greasyfork.org/scripts/23181-colorpicker/code/colorPicker.js?version=147862
  13. // @run-at document-idle
  14. // @author Rob Garrison
  15. // ==/UserScript==
  16. (() => {
  17. "use strict";
  18.  
  19. GM_addStyle("div.cp-app { margin-top:65px; z-index:10; }");
  20.  
  21. function addPicker() {
  22. if (document.querySelector(".js-color-editor")) {
  23. jsColorPicker(".js-color-editor-input", {
  24. customBG: "#222",
  25. noAlpha: true,
  26. renderCallback: function(colors) {
  27. let input = this && this.input;
  28. if (input) {
  29. input.value = "#" + colors.HEX;
  30. input.previousElementSibling.style.backgroundColor = input.value;
  31. }
  32. }
  33. });
  34. }
  35. }
  36.  
  37. /* replace colorPicker storage */
  38. window.ColorPicker.docCookies = (key, val) => {
  39. if (typeof val === "undefined") {
  40. return GM_getValue(key);
  41. }
  42. GM_setValue(key, val);
  43. };
  44.  
  45. /* colorPickerMemosNoAlpha *MUST* follow this format
  46. "'rgba(83,25,231,1)','rgba(86,66,66,1)','rgba(22,20,223,1)'"
  47. */
  48. function convertColorsToRgba(values) {
  49. let result = [];
  50. // see http://stackoverflow.com/a/26196012/145346
  51. values
  52. .replace(/['"]/g, "")
  53. .split(/\s*,(?![^()]*(?:\([^()]*\))?\))\s*/g)
  54. .forEach(val => {
  55. let rgb = hexToRgb(val);
  56. if (rgb) {
  57. result.push(`'rgba(${rgb.r},${rgb.g},${rgb.b},1)'`);
  58. } else if (rgb === null && val.indexOf("rgba(") > -1) {
  59. // allow adding rgba() definitions
  60. result.push(`'${val}'`);
  61. }
  62. });
  63. return result.join(",");
  64. }
  65.  
  66. // Modified code from http://stackoverflow.com/a/5624139/145346
  67. function hexToRgb(hex) {
  68. let result,
  69. // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF")
  70. shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
  71. hex = hex.replace(shorthandRegex, (m, r, g, b) => {
  72. return r + r + g + g + b + b;
  73. });
  74. result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  75. return result ? {
  76. r: parseInt(result[1], 16),
  77. g: parseInt(result[2], 16),
  78. b: parseInt(result[3], 16)
  79. } : null;
  80. }
  81.  
  82. // Add GM options
  83. GM_registerMenuCommand(
  84. "Set label ColorPicker swatches (8 HEX or RGBA Max)",
  85. () => {
  86. const colors = GM_getValue("colorPickerMemosNoAlpha", "#000000,#ffffff"),
  87. val = prompt("Set label default colors (8 max):", colors);
  88. if (val !== null && typeof val === "string") {
  89. GM_setValue("colorPickerMemosNoAlpha", convertColorsToRgba(val));
  90. }
  91. }
  92. );
  93.  
  94. document.body.addEventListener("click", event => {
  95. // initialize if "Edit" or "New label" button clicked
  96. // because "Save changes" updates the entire item
  97. if (
  98. event.target && event.target.matches(".js-edit-label, .js-details-target")
  99. ) {
  100. addPicker();
  101. }
  102. });
  103. addPicker();
  104.  
  105. })();