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/27 16:08:57
actually, the more I think about it, does this wor
Pam (message me for reviews)
2016/09/27 23:37:32
It works when I've tested it. Certainly there migh
| |
14 if (info.id === chrome.runtime.id) | |
15 injectContentScripts(); | |
16 }); | |
17 | |
18 // Listen for messages from the content script, so an old version can detect | |
19 // the loss of connection and disable itself when the extension has been | |
20 // updated, disabled, or uninstalled. | |
21 chrome.runtime.onMessage.addListener(function(message, from, reply) { | |
22 reply(); | |
23 }); | |
24 | |
25 // Inject the content scripts into every open tab, on every window. | |
26 function injectContentScripts() { | |
27 var scripts = chrome.runtime.getManifest().content_scripts[0].js; | |
28 | |
29 chrome.tabs.query({}, function(tabs) { | |
30 for (var tab_index = 0; tab_index < tabs.length; ++tab_index) { | |
Devlin
2016/09/27 16:08:57
suggestion:
for (let tab of tabs)
or
tabs.forEach(
Pam (message me for reviews)
2016/09/27 23:37:32
Done.
| |
31 var tab = tabs[tab_index]; | |
32 for (var i = 0; i < scripts.length; ++i) { | |
33 // This will produce an error if extensions are prohibited on the | |
34 // tab (e.g., chrome:// pages), but we can ignore it. | |
35 chrome.tabs.executeScript(tab.id, | |
36 { | |
37 file: scripts[i], | |
38 allFrames: true, | |
39 runAt: 'document_start' | |
40 }); | |
41 } | |
42 } | |
43 }); | |
44 } | |
OLD | NEW |