-
Notifications
You must be signed in to change notification settings - Fork 459
/
background.js
93 lines (81 loc) · 3.29 KB
/
background.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Global variables only exist for the life of the page, so they get reset
// each time the page is unloaded.
var counter = 1;
var lastTabId = -1;
function sendMessage() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
lastTabId = tabs[0].id;
chrome.tabs.sendMessage(lastTabId, "Background page started.");
});
}
sendMessage();
chrome.browserAction.setBadgeText({text: "ON"});
console.log("Loaded.");
chrome.runtime.onInstalled.addListener(function() {
console.log("Installed.");
// localStorage is persisted, so it's a good place to keep state that you
// need to persist across page reloads.
localStorage.counter = 1;
// Register a webRequest rule to redirect bing to google.
var wr = chrome.declarativeWebRequest;
chrome.declarativeWebRequest.onRequest.addRules([{
id: "0",
conditions: [new wr.RequestMatcher({url: {hostSuffix: "bing.com"}})],
actions: [new wr.RedirectRequest({redirectUrl: "http://google.com"})]
}]);
});
chrome.bookmarks.onRemoved.addListener(function(id, info) {
alert("I never liked that site anyway.");
});
chrome.browserAction.onClicked.addListener(function() {
// The event page will unload after handling this event (assuming nothing
// else is keeping it awake). The content script will become the main way to
// interact with us.
chrome.tabs.create({url: "http://google.com"}, function(tab) {
chrome.tabs.executeScript(tab.id, {file: "content.js"}, function() {
// Note: we also sent a message above, upon loading the event page,
// but the content script will not be loaded at that point, so we send
// another here.
sendMessage();
});
});
});
chrome.commands.onCommand.addListener(function(command) {
chrome.tabs.create({url: "http://www.google.com/"});
});
chrome.runtime.onMessage.addListener(function(msg, _, sendResponse) {
if (msg.setAlarm) {
// For testing only. delayInMinutes will be rounded up to at least 1 in a
// packed or released extension.
chrome.alarms.create({delayInMinutes: 0.1});
} else if (msg.delayedResponse) {
// Note: setTimeout itself does NOT keep the page awake. We return true
// from the onMessage event handler, which keeps the message channel open -
// in turn keeping the event page awake - until we call sendResponse.
setTimeout(function() {
sendResponse("Got your message.");
}, 5000);
return true;
} else if (msg.getCounters) {
sendResponse({counter: counter++,
persistentCounter: localStorage.counter++});
}
// If we don't return anything, the message channel will close, regardless
// of whether we called sendResponse.
});
chrome.alarms.onAlarm.addListener(function() {
alert("Time's up!");
});
chrome.runtime.onSuspend.addListener(function() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
// After the unload event listener runs, the page will unload, so any
// asynchronous callbacks will not fire.
alert("This does not show up.");
});
console.log("Unloading.");
chrome.browserAction.setBadgeText({text: ""});
chrome.tabs.sendMessage(lastTabId, "Background page unloaded.");
});