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