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() { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 chrome.tabs.create({url: "http://google.com"}, function(tab) { | 45 chrome.tabs.create({url: "http://google.com"}, function(tab) { |
46 chrome.tabs.executeScript(tab.id, {file: "content.js"}, function() { | 46 chrome.tabs.executeScript(tab.id, {file: "content.js"}, function() { |
47 // Note: we also sent a message above, upon loading the event page, | 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 | 48 // but the content script will not be loaded at that point, so we send |
49 // another here. | 49 // another here. |
50 sendMessage(); | 50 sendMessage(); |
51 }); | 51 }); |
52 }); | 52 }); |
53 }); | 53 }); |
54 | 54 |
55 chrome.experimental.keybinding.onCommand.addListener(function(command) { | 55 chrome.commands.onCommand.addListener(function(command) { |
56 chrome.tabs.create({url: "http://www.google.com/"}); | 56 chrome.tabs.create({url: "http://www.google.com/"}); |
57 }); | 57 }); |
58 | 58 |
59 chrome.runtime.onMessage.addListener(function(msg, _, sendResponse) { | 59 chrome.runtime.onMessage.addListener(function(msg, _, sendResponse) { |
60 if (msg.setAlarm) { | 60 if (msg.setAlarm) { |
| 61 // For testing only. delayInMinutes will be rounded up to at least 5 in a |
| 62 // packed or released extension. |
61 chrome.alarms.create({delayInMinutes: 0.1}); | 63 chrome.alarms.create({delayInMinutes: 0.1}); |
62 } else if (msg.delayedResponse) { | 64 } else if (msg.delayedResponse) { |
63 // Note: setTimeout itself does NOT keep the page awake. We return true | 65 // Note: setTimeout itself does NOT keep the page awake. We return true |
64 // from the onMessage event handler, which keeps the message channel open - | 66 // from the onMessage event handler, which keeps the message channel open - |
65 // in turn keeping the event page awake - until we call sendResponse. | 67 // in turn keeping the event page awake - until we call sendResponse. |
66 setTimeout(function() { | 68 setTimeout(function() { |
67 sendResponse("Got your message."); | 69 sendResponse("Got your message."); |
68 }, 5000); | 70 }, 5000); |
69 return true; | 71 return true; |
70 } else if (msg.getCounters) { | 72 } else if (msg.getCounters) { |
(...skipping 11 matching lines...) Expand all Loading... |
82 chrome.runtime.onSuspend.addListener(function() { | 84 chrome.runtime.onSuspend.addListener(function() { |
83 chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { | 85 chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { |
84 // After the unload event listener runs, the page will unload, so any | 86 // After the unload event listener runs, the page will unload, so any |
85 // asynchronous callbacks will not fire. | 87 // asynchronous callbacks will not fire. |
86 alert("This does not show up."); | 88 alert("This does not show up."); |
87 }); | 89 }); |
88 console.log("Unloading."); | 90 console.log("Unloading."); |
89 chrome.browserAction.setBadgeText({text: ""}); | 91 chrome.browserAction.setBadgeText({text: ""}); |
90 chrome.tabs.sendMessage(lastTabId, "Background page unloaded."); | 92 chrome.tabs.sendMessage(lastTabId, "Background page unloaded."); |
91 }); | 93 }); |
OLD | NEW |