• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2017 the V8 project 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
5(function linkClickerBackgroundScript() {
6
7  // time in ms.
8  let minInterval = 1*1000;
9  let maxInterval = 20*1000;
10  let pattern = /.*/;
11  let enabled = false;
12
13  let animateIconIntervalId;
14
15  // ===========================================================================
16
17  chrome.runtime.onMessage.addListener(function(msg, sender, response) {
18    let result;
19    if (msg.type == 'update') result = updateFromMessage(msg);
20    if (msg.type == 'get') result = getValues();
21    response(result);
22  });
23
24  // ===========================================================================
25  function updateFromMessage(msg) {
26    console.log(msg);
27    minInterval = Number(msg.minInterval)
28    maxInterval = Number(msg.maxInterval);
29    if (maxInterval < minInterval) {
30      let tmpMin = Math.min(minInterval, maxInterval);
31      maxInterval = Math.max(minInterval, maxInterval);
32      minInterval = tmpMin;
33    }
34    pattern = new RegExp(msg.pattern);
35    enabled = Boolean(msg.enabled);
36    updateTabs();
37    scheduleIconAnimation();
38    return getValues();
39  }
40
41  function getValues() {
42    return {
43      type: 'update',
44      minInterval: minInterval,
45      maxInterval: maxInterval,
46      pattern: pattern.source,
47      enabled: enabled
48    }
49  }
50
51  function updateTabs() {
52    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
53      let message = getValues();
54      for (let i = 0; i < tabs.length; ++i) {
55        chrome.tabs.sendMessage(tabs[i].id, message);
56      }
57    });
58  }
59
60  let animationIndex = 0;
61  function animateIcon() {
62    animationIndex = (animationIndex + 1) % 4;
63    chrome.browserAction.setBadgeText( { text: ".".repeat(animationIndex) } );
64  }
65
66  function scheduleIconAnimation() {
67    chrome.browserAction.setBadgeText( { text: "" } );
68    clearInterval(animateIconIntervalId);
69    if (enabled) {
70      animateIconIntervalId = setInterval(animateIcon, 500);
71    }
72  }
73
74})();
75