Browser extension to watch YouTube videos without the distracting cruft around it, in the full window.

youtubecinema/ background.js
86 lines
2.9 KiB

  1. // Be compatible with Chrome|ium. We do not need the full webextension-polyfill.
  2. if (typeof browser === 'undefined') {
  3. this.browser = chrome
  4. };
  5. var cruftedUrlPattern = /^(https?):\/\/(?:.+\.)youtube\.com\/watch\?.*v=([^&#]+)/;
  6. var embeddableUrlPattern = /^(https?):\/\/(?:.+\.)youtube\.com\/embed\/([^&?#]+)/;
  7. // Keep a set of tabIds for which the user disabled the cinema mode (with the pageAction button)
  8. var tabsWithCinemaModeDisabled = {};
  9. // Turn a video url into its embeddable (full-window) version (and vice versa, if bothDirections==true).
  10. function makeNewUrl(tabId, url, bothDirections) {
  11. if (!tabsWithCinemaModeDisabled[tabId]) {
  12. var match = url.match(cruftedUrlPattern);
  13. if (!match)
  14. return;
  15. var scheme = match[1];
  16. var videoId = match[2];
  17. return scheme + '://www.youtube.com/embed/' + videoId + '?rel=0&autoplay=1';
  18. }
  19. else if (bothDirections) {
  20. var match = url.match(embeddableUrlPattern);
  21. if (!match)
  22. return;
  23. var scheme = match[1];
  24. var videoId = match[2];
  25. return scheme + '://www.youtube.com/watch?v=' + videoId;
  26. }
  27. }
  28. // Redirect crufted videos to their full-window version.
  29. function onBeforeRequestListener(details) {
  30. var newUrl = makeNewUrl(details.tabId, details.url, false)
  31. // From the embed, enable one to follow the "watch on YouTube" link (on browsers that pass originUrl)
  32. if (newUrl === details.originUrl)
  33. return
  34. return {redirectUrl: newUrl};
  35. }
  36. browser.webRequest.onBeforeRequest.addListener(
  37. onBeforeRequestListener,
  38. {urls: ["*://*.youtube.com/watch*"]},
  39. ['blocking']
  40. );
  41. // Show the pageAction button if looking at a video (either with or without cruft).
  42. browser.webNavigation.onCommitted.addListener(handleNavigation);
  43. browser.webNavigation.onHistoryStateUpdated.addListener(handleNavigation);
  44. function handleNavigation(details) {
  45. // Ignore pages inside frames.
  46. if (details.frameId !== 0)
  47. return;
  48. // Check if the page is a youtube video (either with or without cruft)
  49. var matchEmbeddable = details.url.match(embeddableUrlPattern);
  50. var matchCruft = details.url.match(cruftedUrlPattern);
  51. if (matchEmbeddable || matchCruft) {
  52. // Show the pageAction button.
  53. browser.pageAction.show(details.tabId);
  54. // In Chrome|ium, listeners stay across page changes, in Firefox they don't. So check first.
  55. if (!browser.pageAction.onClicked.hasListener(handlePageAction))
  56. browser.pageAction.onClicked.addListener(handlePageAction);
  57. }
  58. }
  59. // Enable/Disable cinema mode when the pageAction button is clicked.
  60. function handlePageAction(tab) {
  61. var matchEmbeddable = tab.url.match(embeddableUrlPattern);
  62. if (matchEmbeddable)
  63. tabsWithCinemaModeDisabled[tab.id] = true;
  64. else
  65. delete tabsWithCinemaModeDisabled[tab.id];
  66. // Relocate this page to reflect the new mode.
  67. var newUrl = makeNewUrl(tab.id, tab.url, true)
  68. if (newUrl)
  69. browser.tabs.update(tab.id, {url: newUrl});
  70. }