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