• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5request = {action: 'should_scroll'}
6
7var PLAY_MUSIC_HOSTNAME = 'play.google.com';
8
9function report_scrolling_to_test() {
10  //Sends message to PLT informing that user is scrolling on the browser.
11  var scroll_url = 'http://localhost:8001/scroll';
12  var req = new XMLHttpRequest();
13  req.open('GET', scroll_url, true);
14  req.send("");
15}
16
17
18chrome.runtime.onMessage.addListener(
19  function(message, sender, callback) {
20    if (message == "numberOfVideosPlaying") {
21      callback(numberOfVideosPlaying());
22    }
23 });
24
25function numberOfVideosPlaying() {
26  let number_of_videos_playing = 0;
27  for (let video of document.querySelectorAll('video')) {
28    if (!video.paused) {
29      number_of_videos_playing++;
30    }
31  }
32
33  return number_of_videos_playing;
34}
35
36//Sends message to the test.js(background script). test.js on
37//receiving a message from content script assumes the page has
38//loaded successfully. It further responds with instructions on
39//whether/how to scroll.
40function sendSuccessToBGScript() {
41  chrome.runtime.sendMessage(request, function(response) {
42    if (response && response.should_scroll) {
43      window.focus();
44      lastOffset = window.pageYOffset;
45      var start_interval = Math.max(1000, response.scroll_interval);
46      function smoothScrollDown() {
47        report_scrolling_to_test();
48        window.scrollBy(0, response.scroll_by);
49        if (window.pageYOffset != lastOffset) {
50          lastOffset = window.pageYOffset;
51          setTimeout(smoothScrollDown, response.scroll_interval);
52        } else if (response.should_scroll_up) {
53          setTimeout(smoothScrollUp, start_interval);
54        }
55      }
56      function smoothScrollUp() {
57        report_scrolling_to_test();
58        window.scrollBy(0, -1 * response.scroll_by);
59        if (window.pageYOffset != lastOffset) {
60          lastOffset = window.pageYOffset;
61          setTimeout(smoothScrollUp, response.scroll_interval);
62        } else if (response.scroll_loop) {
63          setTimeout(smoothScrollDown, start_interval);
64        }
65      }
66      setTimeout(smoothScrollDown, start_interval);
67    }
68  });
69}
70
71function afterLoad() {
72  if (document.location.hostname !== PLAY_MUSIC_HOSTNAME) {
73    sendSuccessToBGScript();
74    return;
75  }
76
77  var playButton = document.querySelector('[data-id="play"]');
78
79  //If play music website, if we do not see a play button
80  //that effectively means the music is not loaded. So do not
81  //report success load to test.js.
82  if (playButton) {
83    sendSuccessToBGScript();
84    playButton.click();
85  }
86}
87
88window.addEventListener('load', afterLoad);
89