• 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//Sends message to the test.js(background script). test.js on
18//receiving a message from content script assumes the page has
19//loaded successfully. It further responds with instructions on
20//whether/how to scroll.
21function sendSuccessToBGScript() {
22  chrome.runtime.sendMessage(request, function(response) {
23    if (response && response.should_scroll) {
24      window.focus();
25      lastOffset = window.pageYOffset;
26      var start_interval = Math.max(1000, response.scroll_interval);
27      function smoothScrollDown() {
28        report_scrolling_to_test();
29        window.scrollBy(0, response.scroll_by);
30        if (window.pageYOffset != lastOffset) {
31          lastOffset = window.pageYOffset;
32          setTimeout(smoothScrollDown, response.scroll_interval);
33        } else if (response.should_scroll_up) {
34          setTimeout(smoothScrollUp, start_interval);
35        }
36      }
37      function smoothScrollUp() {
38        report_scrolling_to_test();
39        window.scrollBy(0, -1 * response.scroll_by);
40        if (window.pageYOffset != lastOffset) {
41          lastOffset = window.pageYOffset;
42          setTimeout(smoothScrollUp, response.scroll_interval);
43        } else if (response.scroll_loop) {
44          setTimeout(smoothScrollDown, start_interval);
45        }
46      }
47      setTimeout(smoothScrollDown, start_interval);
48    }
49  });
50}
51
52function afterLoad() {
53  if (document.location.hostname !== PLAY_MUSIC_HOSTNAME) {
54    sendSuccessToBGScript();
55    return;
56  }
57
58  var playButton = document.querySelector('[data-id="play"]');
59
60  //If play music website, if we do not see a play button
61  //that effectively means the music is not loaded. So do not
62  //report success load to test.js.
63  if (playButton) {
64    sendSuccessToBGScript();
65    playButton.click();
66  }
67}
68
69window.addEventListener('load', afterLoad);
70