From: csagan5 <32685696+csagan5@users.noreply.github.com>
Date: Sun, 18 Feb 2018 22:15:25 +0100
Subject: Play videos in background

Break Page Visibility API and Fullscreen API for youtube.com and vimeo.com
Original Javascript code by timdream
---
 third_party/blink/renderer/core/dom/BUILD.gn            |  1 +
 third_party/blink/renderer/core/dom/document.cc         | 17 +++++++++++++----
 .../blink/renderer/core/dom/extensions/video_bg_play.h  |  6 ++++++
 3 files changed, 20 insertions(+), 4 deletions(-)
 create mode 100644 third_party/blink/renderer/core/dom/extensions/video_bg_play.h

diff --git a/third_party/blink/renderer/core/dom/BUILD.gn b/third_party/blink/renderer/core/dom/BUILD.gn
--- a/third_party/blink/renderer/core/dom/BUILD.gn
+++ b/third_party/blink/renderer/core/dom/BUILD.gn
@@ -148,6 +148,7 @@ blink_core_sources("dom") {
     "events/window_event_context.cc",
     "events/window_event_context.h",
     "extensions/dont-track-me.h",
+    "extensions/video-bg-play.h",
     "exception_code.h",
     "first_letter_pseudo_element.cc",
     "first_letter_pseudo_element.h",
diff --git a/third_party/blink/renderer/core/dom/document.cc b/third_party/blink/renderer/core/dom/document.cc
--- a/third_party/blink/renderer/core/dom/document.cc
+++ b/third_party/blink/renderer/core/dom/document.cc
@@ -221,6 +221,7 @@
 #include "third_party/blink/renderer/core/page/scrolling/scrolling_coordinator.h"
 #include "third_party/blink/renderer/core/page/scrolling/snap_coordinator.h"
 #include "third_party/blink/renderer/core/page/scrolling/top_document_root_scroller_controller.h"
+#include "extensions/video_bg_play.h"
 #include "third_party/blink/renderer/core/paint/compositing/paint_layer_compositor.h"
 #include "third_party/blink/renderer/core/paint/paint_layer_scrollable_area.h"
 #include "third_party/blink/renderer/core/policy/document_policy.h"
@@ -5848,17 +5849,25 @@ void Document::FinishedParsing() {
 
   // determine whether this is a Google search results page
   const SecurityOrigin *origin = GetSecurityOrigin();
-  if (origin) {
+  auto* bodyElement = body();
+  if (origin && bodyElement) {
     WTF::String domain = origin->Domain();
     size_t pos = domain.Find(".google.");
-    auto* bodyElement = body();
-    if (bodyElement && (pos != WTF::kNotFound) && (domain.length() - pos - 8 < 4)) {
+    if ((pos != WTF::kNotFound) && (domain.length() - pos - 8 < 4)) {
       LOG(INFO) << "injecting anti-AMP-cure Javascript payload";
       HTMLScriptElement* e = HTMLScriptElement::Create(*this, CreateElementFlags());
       e->setText(ANTI_AMP_CURE_JS);
       bodyElement->AppendChild(e);
     }
-  }
+
+    // check for eligibility of the video bg fix
+    if ((WTF::kNotFound != domain.Find("youtube.com")) || (WTF::kNotFound != domain.Find("vimeo.com"))) {
+      LOG(INFO) << "injecting video-bg-play Javascript payload";
+      HTMLScriptElement* e = HTMLScriptElement::Create(*this, CreateElementFlags());
+      e->setText(VIDEO_BG_PLAY_JS);
+      bodyElement->AppendChild(e);
+    }
+  } // has origin and body element
 }
 
 void Document::ElementDataCacheClearTimerFired(TimerBase*) {
diff --git a/third_party/blink/renderer/core/dom/extensions/video_bg_play.h b/third_party/blink/renderer/core/dom/extensions/video_bg_play.h
new file mode 100644
--- /dev/null
+++ b/third_party/blink/renderer/core/dom/extensions/video_bg_play.h
@@ -0,0 +1,6 @@
+#ifndef video_bg_play_h
+#define video_bg_play_h
+
+#define VIDEO_BG_PLAY_JS "'use strict';\n\n/* video background play fix - original version by timdream */\ndocument.videoBGFix = {};\n\n// Page Visibility API\nObject.defineProperties(document.videoBGFix,\n  { 'hidden': {value: false}, 'visibilityState': {value: 'visible'} });\n\nwindow.addEventListener(\n  'visibilitychange', evt => evt.stopImmediatePropagation(), true);\nwindow.addEventListener(\n  'blur', evt => evt.stopImmediatePropagation(), true);\n\n// Fullscreen API\nwindow.addEventListener('fullscreenchange', evt => {\n  Object.defineProperties(document.videoBGFix,\n    { 'fullscreenEnabled': {value: true},\n      'fullscreen': {value: true},\n      'fullscreenElement': {value: document.fullscreenElement.videoBGFix}});\n  window.addEventListener(\n    'fullscreenchange', evt => evt.stopImmediatePropagation(), true);\n}, { capture: true, once: true });\n"
+
+#endif  // video_bg_play_h
-- 
2.7.4

