content.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. (function () {
  2. console.log("HELLO FROM PLAYLIST!", Math.floor(Math.random() * 999));
  3. function msgPlayEvt(type, target) {
  4. const payload = {
  5. type,
  6. url: window.location.href,
  7. timestamp: target.currentTime,
  8. duration: target.duration,
  9. title: document.title.replace(/ - YouTube$/, ""),
  10. };
  11. return browser.runtime.sendMessage(payload);
  12. }
  13. const vid = document.querySelector("video");
  14. if (vid) {
  15. vid.addEventListener("play", function (evt) {
  16. msgPlayEvt("play", evt.target);
  17. });
  18. vid.addEventListener("playing", function (evt) {
  19. msgPlayEvt("playing", evt.target);
  20. });
  21. vid.addEventListener("pause", function (evt) {
  22. msgPlayEvt("pause", evt.target);
  23. });
  24. vid.addEventListener("ended", function (evt) {
  25. msgPlayEvt("ended", evt.target);
  26. });
  27. }
  28. // Listen for messages from the popup or background script
  29. browser.runtime.onMessage.addListener((message) => {
  30. if (message.type === "print") {
  31. console.log("MESSAGE", message);
  32. }
  33. if (message.type === "autoplay") {
  34. console.log('press play');
  35. const play = document.querySelector(
  36. 'button.ytp-play-button.ytp-button[data-title-no-tooltip="Play"]',
  37. );
  38. if (play) {
  39. play.click();
  40. }
  41. }
  42. });
  43. })();