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) { |
| 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 tabs.forEach(function(tab) { |
| 31 scripts.forEach(function(script) { |
| 32 // This will produce an error if extensions are prohibited on the |
| 33 // tab (e.g., chrome:// pages), but we can ignore it. |
| 34 chrome.tabs.executeScript(tab.id, |
| 35 { |
| 36 file: script, |
| 37 allFrames: true, |
| 38 runAt: 'document_start' |
| 39 }); |
| 40 }); |
| 41 }); |
| 42 }); |
| 43 } |
OLD | NEW |