summaryrefslogtreecommitdiff
path: root/browsers/firefox/extension/background.js
blob: df0ee80a5964ed0d2d032b97a8282da35f6f4750 (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
let port = browser.runtime.connectNative("accent_color");
let current = null;

function broadcast(color) {
  browser.tabs.query({}).then(tabs => {
    for (let tab of tabs) {
      browser.tabs.sendMessage(tab.id, {color}).catch(() => {});
    }
  });
  browser.theme.update({colors: {toolbar: color}});
}

port.onMessage.addListener(msg => {
  if (msg.color && msg.color !== current) {
    current = msg.color;
    broadcast(current);
  }
});

function poll() {
  try {
    port.postMessage({query: "color"});
  } catch (e) {
    // ignore
  }
}
setInterval(poll, 1000);
poll();

browser.runtime.onMessage.addListener((msg, sender) => {
  if (msg.request === "color") {
    return Promise.resolve({color: current});
  }
});