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 25 matching lines...) Expand all Loading... |
36 chrome.tabs.executeScript({file: "content.js"}, function() { | 36 chrome.tabs.executeScript({file: "content.js"}, function() { |
37 // Note: we also sent a message above, upon loading the transient page, | 37 // 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 | 38 // but the content script will not be loaded at that point, so we send |
39 // another here. | 39 // another here. |
40 sendMessage(); | 40 sendMessage(); |
41 }); | 41 }); |
42 }); | 42 }); |
43 | 43 |
44 chrome.extension.onMessage.addListener(function(msg, _, sendResponse) { | 44 chrome.extension.onMessage.addListener(function(msg, _, sendResponse) { |
45 if (msg.setAlarm) { | 45 if (msg.setAlarm) { |
46 chrome.experimental.alarms.create({delayInSeconds: 5}); | 46 chrome.experimental.alarms.create({delayInMinutes: 0.1}); |
47 } else if (msg.delayedResponse) { | 47 } else if (msg.delayedResponse) { |
48 // Note: setTimeout itself does NOT keep the page awake. We return true | 48 // Note: setTimeout itself does NOT keep the page awake. We return true |
49 // from the onMessage event handler, which keeps the message channel open - | 49 // from the onMessage event handler, which keeps the message channel open - |
50 // in turn keeping the transient page awake - until we call sendResponse. | 50 // in turn keeping the transient page awake - until we call sendResponse. |
51 setTimeout(function() { | 51 setTimeout(function() { |
52 sendResponse("Got your message."); | 52 sendResponse("Got your message."); |
53 }, 5000); | 53 }, 5000); |
54 return true; | 54 return true; |
55 } else if (msg.getCounters) { | 55 } else if (msg.getCounters) { |
56 sendResponse({counter: counter++, | 56 sendResponse({counter: counter++, |
(...skipping 11 matching lines...) Expand all Loading... |
68 function() { | 68 function() { |
69 chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { | 69 chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { |
70 // After the unload event listener runs, the page will unload, so any | 70 // After the unload event listener runs, the page will unload, so any |
71 // asynchronous callbacks will not fire. | 71 // asynchronous callbacks will not fire. |
72 alert("This does not show up."); | 72 alert("This does not show up."); |
73 }); | 73 }); |
74 console.log("Unloading."); | 74 console.log("Unloading."); |
75 chrome.browserAction.setBadgeText({text: ""}); | 75 chrome.browserAction.setBadgeText({text: ""}); |
76 chrome.tabs.sendMessage(lastTabId, "Background page unloaded."); | 76 chrome.tabs.sendMessage(lastTabId, "Background page unloaded."); |
77 }); | 77 }); |
OLD | NEW |