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.experimental.runtime.onInstalled.addListener(function() { |
| 22 console.log("Installed."); |
| 23 |
22 // 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 |
23 // need to persist across page reloads. | 25 // need to persist across page reloads. |
24 localStorage.counter = 1; | 26 localStorage.counter = 1; |
25 console.log("Installed."); | 27 |
| 28 // Register a webRequest rule to redirect bing to google. |
| 29 var wr = chrome.experimental.webRequest; |
| 30 chrome.experimental.webRequest.onRequest.addRules([{ |
| 31 id: "0", |
| 32 conditions: [new wr.RequestMatcher({url: {hostSuffix: "bing.com"}})], |
| 33 actions: [new wr.RedirectRequest({redirectUrl: "http://google.com"})] |
| 34 }]); |
26 }); | 35 }); |
27 | 36 |
28 chrome.bookmarks.onRemoved.addListener(function(id, info) { | 37 chrome.bookmarks.onRemoved.addListener(function(id, info) { |
29 alert("I never liked that site anyway."); | 38 alert("I never liked that site anyway."); |
30 }); | 39 }); |
31 | 40 |
32 chrome.browserAction.onClicked.addListener(function() { | 41 chrome.browserAction.onClicked.addListener(function() { |
33 // The transient page will unload after handling this event (assuming nothing | 42 // The transient page will unload after handling this event (assuming nothing |
34 // else is keeping it awake). The content script will become the main way to | 43 // else is keeping it awake). The content script will become the main way to |
35 // interact with us. | 44 // interact with us. |
36 chrome.tabs.executeScript({file: "content.js"}, function() { | 45 chrome.tabs.executeScript({file: "content.js"}, function() { |
37 // Note: we also sent a message above, upon loading the transient page, | 46 // Note: we also sent a message above, upon loading the transient page, |
38 // but the content script will not be loaded at that point, so we send | 47 // but the content script will not be loaded at that point, so we send |
39 // another here. | 48 // another here. |
40 sendMessage(); | 49 sendMessage(); |
41 }); | 50 }); |
42 }); | 51 }); |
43 | 52 |
| 53 chrome.experimental.keybinding.onCommand.addListener(function(command) { |
| 54 chrome.tabs.create({url: "http://www.google.com/"}); |
| 55 }); |
| 56 |
44 chrome.extension.onMessage.addListener(function(msg, _, sendResponse) { | 57 chrome.extension.onMessage.addListener(function(msg, _, sendResponse) { |
45 if (msg.setAlarm) { | 58 if (msg.setAlarm) { |
46 chrome.experimental.alarms.create({delayInSeconds: 5}); | 59 chrome.experimental.alarms.create({delayInSeconds: 5}); |
47 } else if (msg.delayedResponse) { | 60 } else if (msg.delayedResponse) { |
48 // Note: setTimeout itself does NOT keep the page awake. We return true | 61 // Note: setTimeout itself does NOT keep the page awake. We return true |
49 // from the onMessage event handler, which keeps the message channel open - | 62 // from the onMessage event handler, which keeps the message channel open - |
50 // in turn keeping the transient page awake - until we call sendResponse. | 63 // in turn keeping the transient page awake - until we call sendResponse. |
51 setTimeout(function() { | 64 setTimeout(function() { |
52 sendResponse("Got your message."); | 65 sendResponse("Got your message."); |
53 }, 5000); | 66 }, 5000); |
(...skipping 14 matching lines...) Expand all Loading... |
68 function() { | 81 function() { |
69 chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { | 82 chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { |
70 // After the unload event listener runs, the page will unload, so any | 83 // After the unload event listener runs, the page will unload, so any |
71 // asynchronous callbacks will not fire. | 84 // asynchronous callbacks will not fire. |
72 alert("This does not show up."); | 85 alert("This does not show up."); |
73 }); | 86 }); |
74 console.log("Unloading."); | 87 console.log("Unloading."); |
75 chrome.browserAction.setBadgeText({text: ""}); | 88 chrome.browserAction.setBadgeText({text: ""}); |
76 chrome.tabs.sendMessage(lastTabId, "Background page unloaded."); | 89 chrome.tabs.sendMessage(lastTabId, "Background page unloaded."); |
77 }); | 90 }); |
OLD | NEW |