Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(249)

Side by Side Diff: chrome/common/extensions/docs/examples/api/transientPage/basic/background.js

Issue 10280005: Fix the event page sample extension to say "event page". (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: docgen Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 // Global variables only exist for the life of the page, so they get reset
6 // each time the page is unloaded.
7 var counter = 1;
8
9 var lastTabId = -1;
10 function sendMessage() {
11 chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
12 lastTabId = tabs[0].id;
13 chrome.tabs.sendMessage(lastTabId, "Background page started.");
14 });
15 }
16
17 sendMessage();
18 chrome.browserAction.setBadgeText({text: "ON"});
19 console.log("Loaded.");
20
21 chrome.experimental.runtime.onInstalled.addListener(function() {
22 console.log("Installed.");
23
24 // localStorage is persisted, so it's a good place to keep state that you
25 // need to persist across page reloads.
26 localStorage.counter = 1;
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 }]);
35 });
36
37 chrome.bookmarks.onRemoved.addListener(function(id, info) {
38 alert("I never liked that site anyway.");
39 });
40
41 chrome.browserAction.onClicked.addListener(function() {
42 // The transient page will unload after handling this event (assuming nothing
43 // else is keeping it awake). The content script will become the main way to
44 // interact with us.
45 chrome.tabs.executeScript({file: "content.js"}, function() {
46 // Note: we also sent a message above, upon loading the transient page,
47 // but the content script will not be loaded at that point, so we send
48 // another here.
49 sendMessage();
50 });
51 });
52
53 chrome.experimental.keybinding.onCommand.addListener(function(command) {
54 chrome.tabs.create({url: "http://www.google.com/"});
55 });
56
57 chrome.extension.onMessage.addListener(function(msg, _, sendResponse) {
58 if (msg.setAlarm) {
59 chrome.experimental.alarms.create({delayInSeconds: 5});
60 } else if (msg.delayedResponse) {
61 // Note: setTimeout itself does NOT keep the page awake. We return true
62 // from the onMessage event handler, which keeps the message channel open -
63 // in turn keeping the transient page awake - until we call sendResponse.
64 setTimeout(function() {
65 sendResponse("Got your message.");
66 }, 5000);
67 return true;
68 } else if (msg.getCounters) {
69 sendResponse({counter: counter++,
70 persistentCounter: localStorage.counter++});
71 }
72 // If we don't return anything, the message channel will close, regardless
73 // of whether we called sendResponse.
74 });
75
76 chrome.experimental.alarms.onAlarm.addListener(function() {
77 alert("Time's up!");
78 });
79
80 chrome.experimental.runtime.onBackgroundPageUnloadingSoon.addListener(
81 function() {
82 chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
83 // After the unload event listener runs, the page will unload, so any
84 // asynchronous callbacks will not fire.
85 alert("This does not show up.");
86 });
87 console.log("Unloading.");
88 chrome.browserAction.setBadgeText({text: ""});
89 chrome.tabs.sendMessage(lastTabId, "Background page unloaded.");
90 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698