GitHub Label Color Picker

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

目前为 2016-09-17 提交的版本。查看 最新版本

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