notification.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. var browser = browser || chrome;
  2. chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
  3. let value = message.value;
  4. switch (message.subject) {
  5. case "tabsUpdated":
  6. setTimeout(function(blacklist) {
  7. hideVideo(blacklist);
  8. }, 2000, value);
  9. break;
  10. case "updateBlacklist":
  11. hideVideo(value);
  12. default:
  13. }
  14. });
  15. /**
  16. * Hide video in feed matching keywords
  17. * @param array blacklist
  18. */
  19. function hideVideo(blacklist) {
  20. console.debug("Hidding video");
  21. blacklist = blacklist || [];
  22. let regex = new RegExp(blacklist.join('|'), "i");
  23. let shelves = document.getElementsByClassName("item-section");
  24. // Don't match anything
  25. if (blacklist.length < 1) {
  26. regex = new RegExp('$^', 'i');
  27. }
  28. Array.from(shelves).forEach(function(shelf) {
  29. let xpathReturn = document.evaluate('.//div[1]/div[2]/ul/li/div/div/div[1]/div[2]/h3/a/text()', shelf, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
  30. if (xpathReturn.singleNodeValue !== undefined) {
  31. let title = xpathReturn.singleNodeValue.nodeValue;
  32. if (regex.exec(title) !== null) {
  33. // Thhis is necessary to avoid infinite loops with zero-width matches
  34. shelf.style.display = 'none';
  35. } else {
  36. shelf.style.display = 'block';
  37. }
  38. }
  39. });
  40. }