Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

contentScript.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /***********/
  2. /* HELPERS */
  3. /***********/
  4. /* Given the result of a query to storage (for the matching and exclusion
  5. pattern lists, and the current mode - whitelist or blacklist), this function
  6. determines whether stickies should be killed on the currently loaded page.
  7. Stickies are killed if the following is true:
  8. (blacklist mode is active, AND
  9. at least one matching pattern matches the URL of the current page, AND
  10. zero exclusion patterns match the URL of the current page)
  11. OR
  12. (whitelist mode is active, AND
  13. zero exclusion matters match the URL of the current page)
  14. */
  15. function checkForShouldKillSticky(result) {
  16. var shouldKillSticky = false;
  17. /* If whitelist mode is active, then the matching patterns list is treated
  18. as containing a single pattern which will match all possible URLs.
  19. Patterns that are empty strings (i.e., blank lines in the patterns
  20. lists) are ignored, as are any patterns that begin with a pound sign (#)
  21. (this allows for comments in the patterns lists). */
  22. let matchingPatterns = result.mode == "whitelist" ?
  23. [ ".*" ] :
  24. (typeof result.matchingPatterns != "undefined" ?
  25. result.matchingPatterns.split("\n") :
  26. [ ]);
  27. for (let pattern of matchingPatterns) {
  28. if (pattern &&
  29. !pattern.hasPrefix("#") &&
  30. location.href.match(new RegExp(pattern))) {
  31. shouldKillSticky = true;
  32. break;
  33. }
  34. }
  35. let exclusionPatterns = typeof result.exclusionPatterns != "undefined" ?
  36. result.exclusionPatterns.split("\n") :
  37. [ ];
  38. for (let pattern of exclusionPatterns) {
  39. if (pattern &&
  40. !pattern.hasPrefix("#") &&
  41. location.href.match(new RegExp(pattern))) {
  42. shouldKillSticky = false;
  43. break;
  44. }
  45. }
  46. return shouldKillSticky;
  47. }
  48. /* This function sends a message to the background script (background.js),
  49. which then updates the page action icon (i.e., the browser toolbar icon)
  50. to reflect whether killing stickies is enabled on the current page.
  51. */
  52. function updateIcon(shouldKillSticky) {
  53. chrome.runtime.sendMessage({ "killingStickies" : shouldKillSticky });
  54. }
  55. /******************/
  56. /* INITIALIZATION */
  57. /******************/
  58. function initialize() {
  59. chrome.storage.sync.get([ "matchingPatterns", "exclusionPatterns", "mode" ],
  60. (result) => {
  61. let shouldKillSticky = checkForShouldKillSticky(result);
  62. updateIcon(shouldKillSticky);
  63. window.onload = () => {
  64. if (shouldKillSticky) killSticky();
  65. };
  66. });
  67. }
  68. initialize();