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

youtubecinema/extension/ background.js
161 lines
4.7 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. // API docs: https://developers.google.com/youtube/player_parameters
  38. url.searchParams.delete('v');
  39. url.searchParams.set('rel', '0'); // no suggestions after my video, please.
  40. url.searchParams.set('iv_load_policy', '3'); // no video annotations, thanks.
  41. url.searchParams.set('modestbranding', '1'); // no YouTube branding either.
  42. url.searchParams.set('autoplay', '1'); // do play my video! :)
  43. return url.href;
  44. }
  45. function embeddableToCruftedVideoUrl(url) {
  46. url = new URL(url);
  47. var videoId = url.pathname.match(/\/embed\/(.*)/)[1];
  48. url.pathname = '/watch';
  49. url.searchParams.set('v', videoId);
  50. url.searchParams.delete('rel');
  51. url.searchParams.delete('modestbranding');
  52. url.searchParams.delete('iv_load_policy');
  53. url.searchParams.delete('autoplay');
  54. return url.href;
  55. }
  56. // Turn a video url into its embeddable (full-window) version, or vice versa if not in cinema mode.
  57. function makeNewUrl(url, cinemaModeEnabled) {
  58. if (cinemaModeEnabled) {
  59. if (isCruftedVideo(url)) {
  60. return cruftedToEmbeddableVideoUrl(url);
  61. }
  62. }
  63. else {
  64. if (isEmbeddedVideo(url)) {
  65. return embeddableToCruftedVideoUrl(url);
  66. }
  67. }
  68. return undefined;
  69. }
  70. // Redirect crufted videos to their full-window version.
  71. function onBeforeRequestListener(details) {
  72. if (hasCinemaModeEnabled(details.tabId)) {
  73. var newUrl = makeNewUrl(details.url, true);
  74. // Return if we were not visiting a embedded youtube video
  75. if (newUrl === undefined) return;
  76. return {redirectUrl: newUrl};
  77. }
  78. }
  79. browser.webRequest.onBeforeRequest.addListener(
  80. onBeforeRequestListener,
  81. {urls: ["*://*.youtube.com/watch*"]},
  82. ['blocking']
  83. );
  84. // Show the pageAction button if looking at a video (either with or without cruft).
  85. var youtubeUrlFilter = {url: [{hostSuffix: '.youtube.com'}]}
  86. browser.webNavigation.onCommitted.addListener(handleNavigation, youtubeUrlFilter);
  87. browser.webNavigation.onHistoryStateUpdated.addListener(handleNavigation, youtubeUrlFilter);
  88. function handleNavigation(details) {
  89. if (details.frameId !== 0) {
  90. return;
  91. }
  92. // If we are on youtube, show the button.
  93. if (isYoutube(details.url)) {
  94. // Show the pageAction button.
  95. browser.pageAction.show(details.tabId);
  96. setIconActive(details.tabId, appearsToHaveCinemaModeEnabled(details.tabId, details.url))
  97. // In Chrome|ium, listeners stay across page changes, in Firefox they don't. So check first.
  98. if (!browser.pageAction.onClicked.hasListener(handlePageAction))
  99. browser.pageAction.onClicked.addListener(handlePageAction);
  100. }
  101. }
  102. // Enable/Disable cinema mode when the pageAction button is clicked.
  103. function handlePageAction(tab) {
  104. var enable = !appearsToHaveCinemaModeEnabled(tab.id, tab.url)
  105. if (enable) {
  106. delete tabsWithCinemaModeDisabled[tab.id];
  107. } else {
  108. tabsWithCinemaModeDisabled[tab.id] = true;
  109. }
  110. setIconActive(tab.id, enable);
  111. // Relocate this page to reflect the new mode.
  112. var newUrl = makeNewUrl(tab.url, hasCinemaModeEnabled(tab.id));
  113. if (newUrl)
  114. browser.tabs.update(tab.id, {url: newUrl});
  115. }
  116. var activeIcons = {
  117. '19': '/img/icon_active/19.png',
  118. '38': '/img/icon_active/38.png',
  119. '48': '/img/icon_active/48.png',
  120. '96': '/img/icon_active/96.png',
  121. '128': '/img/icon_active/128.png',
  122. }
  123. var inactiveIcons = {
  124. '19': '/img/icon_inactive/19.png',
  125. '38': '/img/icon_inactive/38.png',
  126. '48': '/img/icon_inactive/48.png',
  127. '96': '/img/icon_inactive/96.png',
  128. '128': '/img/icon_inactive/128.png',
  129. }
  130. function setIconActive(tabId, active) {
  131. browser.pageAction.setIcon({
  132. tabId: tabId,
  133. path: active ? activeIcons : inactiveIcons,
  134. })
  135. }