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

youtubecinema/extension/ background.js
156 lines
4.3 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. // Keep a set of tabIds for which the user disabled the cinema mode (with the pageAction button)
  6. var tabsWithCinemaModeDisabled = {};
  7. function hasCinemaModeEnabled(tabId) {
  8. return !tabsWithCinemaModeDisabled[tabId];
  9. }
  10. function appearsToHaveCinemaModeEnabled(tabId, url) {
  11. return (
  12. hasCinemaModeEnabled(tabId)
  13. // When viewing an embedded video with cinema mode disabled, behave as if it was enabled.
  14. || isEmbeddedVideo(url)
  15. );
  16. }
  17. // Our request filters should make use of this test unnecessary, but I prefer to keep it explicit.
  18. function isYoutube(url) {
  19. return new URL(url).hostname.endsWith('.youtube.com');
  20. }
  21. function isEmbeddedVideo(url) {
  22. return (
  23. isYoutube(url)
  24. && new URL(url).pathname.startsWith('/embed/')
  25. );
  26. }
  27. function isCruftedVideo(url) {
  28. return (
  29. isYoutube(url)
  30. && new URL(url).pathname === '/watch'
  31. );
  32. }
  33. function cruftedToEmbeddableVideoUrl(url) {
  34. url = new URL(url);
  35. var videoId = url.searchParams.get('v');
  36. url.pathname = '/embed/' + videoId;
  37. url.searchParams.delete('v');
  38. url.searchParams.set('rel', '0'); // no suggestions after my video, please.
  39. url.searchParams.set('autoplay', '1');
  40. return url.href;
  41. }
  42. function embeddableToCruftedVideoUrl(url) {
  43. url = new URL(url);
  44. var videoId = url.pathname.match(/\/embed\/(.*)/)[1];
  45. url.pathname = '/watch';
  46. url.searchParams.set('v', videoId);
  47. url.searchParams.delete('rel');
  48. url.searchParams.delete('autoplay');
  49. return url.href;
  50. }
  51. // Turn a video url into its embeddable (full-window) version, or vice versa if not in cinema mode.
  52. function makeNewUrl(url, cinemaModeEnabled) {
  53. if (cinemaModeEnabled) {
  54. if (isCruftedVideo(url)) {
  55. return cruftedToEmbeddableVideoUrl(url);
  56. }
  57. }
  58. else {
  59. if (isEmbeddedVideo(url)) {
  60. return embeddableToCruftedVideoUrl(url);
  61. }
  62. }
  63. return undefined;
  64. }
  65. // Redirect crufted videos to their full-window version.
  66. function onBeforeRequestListener(details) {
  67. if (hasCinemaModeEnabled(details.tabId)) {
  68. var newUrl = makeNewUrl(details.url, true);
  69. // Return if we were not visiting a embedded youtube video
  70. if (newUrl === undefined) return;
  71. return {redirectUrl: newUrl};
  72. }
  73. }
  74. browser.webRequest.onBeforeRequest.addListener(
  75. onBeforeRequestListener,
  76. {urls: ["*://*.youtube.com/watch*"]},
  77. ['blocking']
  78. );
  79. // Show the pageAction button if looking at a video (either with or without cruft).
  80. var youtubeUrlFilter = {url: [{hostSuffix: '.youtube.com'}]}
  81. browser.webNavigation.onCommitted.addListener(handleNavigation, youtubeUrlFilter);
  82. browser.webNavigation.onHistoryStateUpdated.addListener(handleNavigation, youtubeUrlFilter);
  83. function handleNavigation(details) {
  84. if (details.frameId !== 0) {
  85. return;
  86. }
  87. // If we are on youtube, show the button.
  88. if (isYoutube(details.url)) {
  89. // Show the pageAction button.
  90. browser.pageAction.show(details.tabId);
  91. setIconActive(details.tabId, appearsToHaveCinemaModeEnabled(details.tabId, details.url))
  92. // In Chrome|ium, listeners stay across page changes, in Firefox they don't. So check first.
  93. if (!browser.pageAction.onClicked.hasListener(handlePageAction))
  94. browser.pageAction.onClicked.addListener(handlePageAction);
  95. }
  96. }
  97. // Enable/Disable cinema mode when the pageAction button is clicked.
  98. function handlePageAction(tab) {
  99. var enable = !appearsToHaveCinemaModeEnabled(tab.id, tab.url)
  100. if (enable) {
  101. delete tabsWithCinemaModeDisabled[tab.id];
  102. } else {
  103. tabsWithCinemaModeDisabled[tab.id] = true;
  104. }
  105. setIconActive(tab.id, enable);
  106. // Relocate this page to reflect the new mode.
  107. var newUrl = makeNewUrl(tab.url, hasCinemaModeEnabled(tab.id));
  108. if (newUrl)
  109. browser.tabs.update(tab.id, {url: newUrl});
  110. }
  111. var activeIcons = {
  112. '19': '/img/icon_active/19.png',
  113. '38': '/img/icon_active/38.png',
  114. '48': '/img/icon_active/48.png',
  115. '96': '/img/icon_active/96.png',
  116. '128': '/img/icon_active/128.png',
  117. }
  118. var inactiveIcons = {
  119. '19': '/img/icon_inactive/19.png',
  120. '38': '/img/icon_inactive/38.png',
  121. '48': '/img/icon_inactive/48.png',
  122. '96': '/img/icon_inactive/96.png',
  123. '128': '/img/icon_inactive/128.png',
  124. }
  125. function setIconActive(tabId, active) {
  126. browser.pageAction.setIcon({
  127. tabId: tabId,
  128. path: active ? activeIcons : inactiveIcons,
  129. })
  130. }