summaryrefslogtreecommitdiff
path: root/chromium/v8/tools/link_clicker.extension/background.js
blob: 43470cb31294325912dcc574bd912d14b8569cb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

(function linkClickerBackgroundScript() {

  // time in ms.
  let minInterval = 1*1000;
  let maxInterval = 20*1000;
  let pattern = /.*/;
  let enabled = false;

  let animateIconIntervalId;

  // ===========================================================================

  chrome.runtime.onMessage.addListener(function(msg, sender, response) {
    let result;
    if (msg.type == 'update') result = updateFromMessage(msg);
    if (msg.type == 'get') result = getValues();
    response(result);
  });

  // ===========================================================================
  function updateFromMessage(msg) {
    console.log(msg);
    minInterval = Number(msg.minInterval)
    maxInterval = Number(msg.maxInterval);
    if (maxInterval < minInterval) {
      let tmpMin = Math.min(minInterval, maxInterval);
      maxInterval = Math.max(minInterval, maxInterval);
      minInterval = tmpMin;
    }
    pattern = new RegExp(msg.pattern);
    enabled = Boolean(msg.enabled);
    updateTabs();
    scheduleIconAnimation();
    return getValues();
  }

  function getValues() {
    return {
      type: 'update',
      minInterval: minInterval,
      maxInterval: maxInterval,
      pattern: pattern.source,
      enabled: enabled
    }
  }

  function updateTabs() {
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
      let message = getValues();
      for (let i = 0; i < tabs.length; ++i) {
        chrome.tabs.sendMessage(tabs[i].id, message);
      }
    });
  }

  let animationIndex = 0;
  function animateIcon() {
    animationIndex = (animationIndex + 1) % 4;
    chrome.browserAction.setBadgeText( { text: ".".repeat(animationIndex) } );
  }

  function scheduleIconAnimation() {
    chrome.browserAction.setBadgeText( { text: "" } );
    clearInterval(animateIconIntervalId);
    if (enabled) {
      animateIconIntervalId = setInterval(animateIcon, 500);
    }
  }

})();