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. 4 5 // Global variables only exist for the life of the page, so they get reset 6 // each time the page is unloaded. 7 var counter = 1; 8 9 var lastTabId = -1; 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."); 14 }); 15 } 16 17 sendMessage(); 18 chrome.browserAction.setBadgeText({text: "ON"}); 19 console.log("Loaded."); 20 21 chrome.runtime.onInstalled.addListener(function() { 22 console.log("Installed."); 23 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; 27 28 // Register a webRequest rule to redirect bing to google. 29 var wr = chrome.declarativeWebRequest; 30 chrome.declarativeWebRequest.onRequest.addRules([{ 31 id: "0", 32 conditions: [new wr.RequestMatcher({url: {hostSuffix: "bing.com"}})], 33 actions: [new wr.RedirectRequest({redirectUrl: "http://google.com"})] 34 }]); 35 }); 36 37 chrome.bookmarks.onRemoved.addListener(function(id, info) { 38 alert("I never liked that site anyway."); 39 }); 40 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 44 // interact with us. 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 49 // another here. 50 sendMessage(); 51 }); 52 }); 53 }); 54 55 chrome.commands.onCommand.addListener(function(command) { 56 chrome.tabs.create({url: "http://www.google.com/"}); 57 }); 58 59 chrome.runtime.onMessage.addListener(function(msg, _, sendResponse) { 60 if (msg.setAlarm) { 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."); 70 }, 5000); 71 return true; 72 } else if (msg.getCounters) { 73 sendResponse({counter: counter++, 74 persistentCounter: localStorage.counter++}); 75 } 76 // If we don't return anything, the message channel will close, regardless 77 // of whether we called sendResponse. 78 }); 79 80 chrome.alarms.onAlarm.addListener(function() { 81 alert("Time's up!"); 82 }); 83 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."); 89 }); 90 console.log("Unloading."); 91 chrome.browserAction.setBadgeText({text: ""}); 92 chrome.tabs.sendMessage(lastTabId, "Background page unloaded."); 93 }); 94