OLD | NEW |
---|---|
1 // Copyright 2016 Google Inc. All rights reserved. | 1 // Copyright 2016 Google Inc. 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 // Put up an informative message on first install. | 5 // Inject the content scripts into all open tabs on first install or update. |
6 chrome.runtime.onInstalled.addListener(function(details) { | 6 chrome.runtime.onInstalled.addListener(function(details) { |
7 if (details.reason == "install") { | 7 if (details.reason == 'install' || details.reason === 'update') |
8 chrome.tabs.create({url: "pages/installed.html"}); | 8 injectContentScripts(); |
9 } | |
10 }); | 9 }); |
10 | |
11 // Inject the content scripts into all open tabs when this extension is | |
12 // re-enabled. | |
13 chrome.management.onEnabled.addListener(function(info) { | |
Devlin
2016/09/19 21:22:31
(Just thinking) Hmm... I wonder if we should make
Pam (message me for reviews)
2016/09/20 21:42:59
I'd star that bug. Perhaps also onDisabled, since
Devlin
2016/09/27 16:08:57
(Orthogonal, but FYI) onDisabled wouldn't make sen
| |
14 if (info.id === chrome.runtime.id) | |
15 injectContentScripts(); | |
16 }); | |
17 | |
18 // Maintain a longstanding connection to the content script, so the old version | |
19 // can disable itself when the extension has been updated, disabled, or | |
20 // uninstalled. | |
Devlin
2016/09/19 21:22:31
This is kind of an abuse of the API. It means we'
Pam (message me for reviews)
2016/09/20 21:42:59
Chrome can't really clean up content scripts, but
Devlin
2016/09/27 16:08:57
Can you file a bug for that?
| |
21 chrome.runtime.onConnect.addListener(function(port) {}); | |
22 | |
23 function injectContentScripts() { | |
24 var scripts = chrome.runtime.getManifest().content_scripts[0].js; | |
Devlin
2016/09/19 21:22:31
given the fact that you're already relying on mani
Pam (message me for reviews)
2016/09/20 21:42:59
True, although the manifest structure is likely to
| |
25 | |
26 chrome.windows.getAll({populate: true}, function(windows) { | |
Devlin
2016/09/19 21:22:31
this would be easier with chrome.tabs.query({}, ta
Pam (message me for reviews)
2016/09/20 21:42:59
Done, thanks (except that I used function() syntax
| |
27 for (var window_index = 0; window_index < windows.length; ++window_index) { | |
28 var win = windows[window_index]; | |
29 for (var tab_index = 0; tab_index < win.tabs.length; ++tab_index) { | |
30 var tab = win.tabs[tab_index]; | |
31 for (var i = 0; i < scripts.length; ++i) { | |
32 chrome.tabs.executeScript(tab.id, | |
33 { | |
34 file: scripts[i], | |
35 allFrames: true, | |
36 runAt: 'document_start' | |
Devlin
2016/09/19 21:22:31
Why document start?
Pam (message me for reviews)
2016/09/20 21:42:59
A number of users want to be able to hit back-back
| |
37 }); | |
38 } | |
39 } | |
40 } | |
41 }); | |
42 } | |
OLD | NEW |