| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Global variables only exist for the life of the page, so they get reset | 5 // Global variables only exist for the life of the page, so they get reset |
| 6 // each time the page is unloaded. | 6 // each time the page is unloaded. |
| 7 var counter = 1; | 7 var counter = 1; |
| 8 | 8 |
| 9 var lastTabId = -1; | 9 var lastTabId = -1; |
| 10 function sendMessage() { | 10 function sendMessage() { |
| 11 chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { | 11 chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { |
| 12 lastTabId = tabs[0].id; | 12 lastTabId = tabs[0].id; |
| 13 chrome.tabs.sendMessage(lastTabId, "Background page started."); | 13 chrome.tabs.sendMessage(lastTabId, "Background page started."); |
| 14 }); | 14 }); |
| 15 } | 15 } |
| 16 | 16 |
| 17 sendMessage(); | 17 sendMessage(); |
| 18 chrome.browserAction.setBadgeText({text: "ON"}); | 18 chrome.browserAction.setBadgeText({text: "ON"}); |
| 19 console.log("Loaded."); | 19 console.log("Loaded."); |
| 20 | 20 |
| 21 chrome.experimental.runtime.onInstalled.addListener(function() { | 21 chrome.runtime.onInstalled.addListener(function() { |
| 22 console.log("Installed."); | 22 console.log("Installed."); |
| 23 | 23 |
| 24 // localStorage is persisted, so it's a good place to keep state that you | 24 // localStorage is persisted, so it's a good place to keep state that you |
| 25 // need to persist across page reloads. | 25 // need to persist across page reloads. |
| 26 localStorage.counter = 1; | 26 localStorage.counter = 1; |
| 27 | 27 |
| 28 // Register a webRequest rule to redirect bing to google. | 28 // Register a webRequest rule to redirect bing to google. |
| 29 var wr = chrome.declarativeWebRequest; | 29 var wr = chrome.declarativeWebRequest; |
| 30 chrome.declarativeWebRequest.onRequest.addRules([{ | 30 chrome.declarativeWebRequest.onRequest.addRules([{ |
| 31 id: "0", | 31 id: "0", |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 persistentCounter: localStorage.counter++}); | 72 persistentCounter: localStorage.counter++}); |
| 73 } | 73 } |
| 74 // If we don't return anything, the message channel will close, regardless | 74 // If we don't return anything, the message channel will close, regardless |
| 75 // of whether we called sendResponse. | 75 // of whether we called sendResponse. |
| 76 }); | 76 }); |
| 77 | 77 |
| 78 chrome.alarms.onAlarm.addListener(function() { | 78 chrome.alarms.onAlarm.addListener(function() { |
| 79 alert("Time's up!"); | 79 alert("Time's up!"); |
| 80 }); | 80 }); |
| 81 | 81 |
| 82 chrome.experimental.runtime.onBackgroundPageUnloadingSoon.addListener( | 82 chrome.runtime.onBackgroundPageUnloadingSoon.addListener( |
| 83 function() { | 83 function() { |
| 84 chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { | 84 chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { |
| 85 // After the unload event listener runs, the page will unload, so any | 85 // After the unload event listener runs, the page will unload, so any |
| 86 // asynchronous callbacks will not fire. | 86 // asynchronous callbacks will not fire. |
| 87 alert("This does not show up."); | 87 alert("This does not show up."); |
| 88 }); | 88 }); |
| 89 console.log("Unloading."); | 89 console.log("Unloading."); |
| 90 chrome.browserAction.setBadgeText({text: ""}); | 90 chrome.browserAction.setBadgeText({text: ""}); |
| 91 chrome.tabs.sendMessage(lastTabId, "Background page unloaded."); | 91 chrome.tabs.sendMessage(lastTabId, "Background page unloaded."); |
| 92 }); | 92 }); |
| OLD | NEW |