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

youtubecinema/ background.js
82 lines
2.8 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(function (details) {maybeShowPageAction(details.tabId, details.url);});
  43. browser.webNavigation.onHistoryStateUpdated.addListener(function (details) {maybeShowPageAction(details.tabId, details.url);});
  44. function maybeShowPageAction(tabId, url) {
  45. var matchEmbeddable = url.match(embeddableUrlPattern);
  46. var matchCruft = url.match(cruftedUrlPattern);
  47. if (matchEmbeddable || matchCruft) {
  48. browser.pageAction.show(tabId);
  49. // In Chrome|ium, listeners stay across page changes, in Firefox they don't. So check first.
  50. if (!browser.pageAction.onClicked.hasListener(handlePageAction))
  51. browser.pageAction.onClicked.addListener(handlePageAction);
  52. }
  53. }
  54. // Enable/Disable cinema mode when the pageAction button is clicked.
  55. function handlePageAction(tab) {
  56. var matchEmbeddable = tab.url.match(embeddableUrlPattern);
  57. if (matchEmbeddable)
  58. tabsWithCinemaModeDisabled[tab.id] = true;
  59. else
  60. delete tabsWithCinemaModeDisabled[tab.id];
  61. // Relocate this page to reflect the new mode.
  62. var newUrl = makeNewUrl(tab.id, tab.url, true)
  63. if (newUrl)
  64. browser.tabs.update(tab.id, {url: newUrl});
  65. }