content.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. console.log("NOW PLAYING (msg here for test)");
  18. });
  19. vid.addEventListener("playing", function (evt) {
  20. msgPlayEvt("playing", evt.target);
  21. console.log("playing... (msg here for test)");
  22. });
  23. vid.addEventListener("pause", function (evt) {
  24. msgPlayEvt("pause", evt.target);
  25. console.log("paused (msg here for test)");
  26. });
  27. vid.addEventListener("ended", function (evt) {
  28. msgPlayEvt("ended", evt.target);
  29. console.log("ON TO THE NEXT ONE! ...but not yet.");
  30. setTimeout(async function () {
  31. // const msg = {
  32. // type: "requestNext",
  33. // //currentVideo: 'something',
  34. // //currentList: 'something'
  35. // };
  36. // const resp = await chrome.runtime.sendMessage(msg);
  37. // console.log("NAVIGATING?", resp);
  38. // if( resp.goto ) {
  39. // console.log("NAVIGATING to", resp.goto);
  40. // window.location = resp.goto;
  41. // console.log("AFTER?");
  42. // }
  43. //TODO: MOVE "NEXT" REQUEST TO BACKGROUND SCRIPT
  44. // (background script will determine if this tab is playing a playlist)
  45. msgPlayEvt("next", evt.target);
  46. }, 3000);
  47. });
  48. console.log("ATTACHED!");
  49. }
  50. // Listen for messages from the popup or background script
  51. browser.runtime.onMessage.addListener((message) => {
  52. console.log("MESSAGE", message);
  53. if (message.type === "print") {
  54. console.log(message);
  55. return Promise.resolve({ status: "Injection completed" });
  56. }
  57. });
  58. })();