1 // Copyright (c) 2012 The Chromium 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.
5 // Global variables only exist for the life of the page, so they get reset
6 // each time the page is unloaded.
10 function sendMessage() {
11 chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
12 lastTabId = tabs[0].id;
13 chrome.tabs.sendMessage(lastTabId, "Background page started.");
18 chrome.browserAction.setBadgeText({text: "ON"});
19 console.log("Loaded.");
21 chrome.runtime.onInstalled.addListener(function() {
22 console.log("Installed.");
24 // localStorage is persisted, so it's a good place to keep state that you
25 // need to persist across page reloads.
26 localStorage.counter = 1;
28 // Register a webRequest rule to redirect bing to google.
29 var wr = chrome.declarativeWebRequest;
30 chrome.declarativeWebRequest.onRequest.addRules([{
32 conditions: [new wr.RequestMatcher({url: {hostSuffix: "bing.com"}})],
33 actions: [new wr.RedirectRequest({redirectUrl: "http://google.com"})]
37 chrome.bookmarks.onRemoved.addListener(function(id, info) {
38 alert("I never liked that site anyway.");
41 chrome.browserAction.onClicked.addListener(function() {
42 // The event page will unload after handling this event (assuming nothing
43 // else is keeping it awake). The content script will become the main way to
45 chrome.tabs.create({url: "http://google.com"}, function(tab) {
46 chrome.tabs.executeScript(tab.id, {file: "content.js"}, function() {
47 // Note: we also sent a message above, upon loading the event page,
48 // but the content script will not be loaded at that point, so we send
55 chrome.commands.onCommand.addListener(function(command) {
56 chrome.tabs.create({url: "http://www.google.com/"});
59 chrome.runtime.onMessage.addListener(function(msg, _, sendResponse) {
61 // For testing only. delayInMinutes will be rounded up to at least 1 in a
62 // packed or released extension.
63 chrome.alarms.create({delayInMinutes: 0.1});
64 } else if (msg.delayedResponse) {
65 // Note: setTimeout itself does NOT keep the page awake. We return true
66 // from the onMessage event handler, which keeps the message channel open -
67 // in turn keeping the event page awake - until we call sendResponse.
68 setTimeout(function() {
69 sendResponse("Got your message.");
72 } else if (msg.getCounters) {
73 sendResponse({counter: counter++,
74 persistentCounter: localStorage.counter++});
76 // If we don't return anything, the message channel will close, regardless
77 // of whether we called sendResponse.
80 chrome.alarms.onAlarm.addListener(function() {
84 chrome.runtime.onSuspend.addListener(function() {
85 chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
86 // After the unload event listener runs, the page will unload, so any
87 // asynchronous callbacks will not fire.
88 alert("This does not show up.");
90 console.log("Unloading.");
91 chrome.browserAction.setBadgeText({text: ""});
92 chrome.tabs.sendMessage(lastTabId, "Background page unloaded.");