You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*******************************/
  2. /* EVENT LISTENER MANIPULATION */
  3. /*******************************/
  4. /* Adds an event listener to a button (or other clickable element), attaching
  5. it to both "click" and "keyup" events (for use with keyboard navigation).
  6. Optionally also attaches the listener to the 'mousedown' event, making the
  7. element activate on mouse down instead of mouse up. */
  8. Element.prototype.addActivateEvent = function(func, includeMouseDown) {
  9. let ael = this.activateEventListener = (event) => { if (event.button === 0 || event.key === ' ') func(event) };
  10. if (includeMouseDown) this.addEventListener("mousedown", ael);
  11. this.addEventListener("click", ael);
  12. this.addEventListener("keyup", ael);
  13. }
  14. /* Removes event listener from a clickable element, automatically detaching it
  15. from all relevant event types. */
  16. Element.prototype.removeActivateEvent = function() {
  17. let ael = this.activateEventListener;
  18. this.removeEventListener("mousedown", ael);
  19. this.removeEventListener("click", ael);
  20. this.removeEventListener("keyup", ael);
  21. }
  22. /*******************/
  23. /* EVENT LISTENERS */
  24. /*******************/
  25. document.querySelectorAll("button").forEach(button => {
  26. button.addActivateEvent((event) => {
  27. event.target.blur();
  28. if (button.classList.contains("save-button"))
  29. saveChanges();
  30. else
  31. resetChanges();
  32. });
  33. });
  34. document.querySelectorAll("textarea").forEach(textarea => {
  35. textarea.addEventListener("input", (event) => {
  36. document.querySelectorAll("button").forEach(button => {
  37. button.disabled = false;
  38. });
  39. });
  40. });
  41. /***********/
  42. /* HELPERS */
  43. /***********/
  44. function saveChanges() {
  45. let matchingPatterns = document.querySelector("#matchingPatterns textarea").value;
  46. let exclusionPatterns = document.querySelector("#exclusionPatterns textarea").value;
  47. chrome.storage.sync.set({
  48. "matchingPatterns": matchingPatterns,
  49. "exclusionPatterns": exclusionPatterns
  50. }, () => {
  51. document.querySelectorAll("button").forEach(button => {
  52. button.disabled = true;
  53. });
  54. });
  55. }
  56. function resetChanges() {
  57. chrome.storage.sync.get([ "matchingPatterns", "exclusionPatterns" ], (result) => {
  58. document.querySelector("#matchingPatterns textarea").value = result.matchingPatterns;
  59. document.querySelector("#exclusionPatterns textarea").value = result.exclusionPatterns;
  60. document.querySelectorAll("button").forEach(button => {
  61. button.disabled = true;
  62. });
  63. });
  64. }
  65. /******************/
  66. /* INITIALIZATION */
  67. /******************/
  68. resetChanges();
  69. document.querySelector("#matchingPatterns textarea").focus();