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 // Inject the content scripts into all open tabs on first install or update. | 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' || details.reason === 'update') | 7 if (details.reason == 'install' || details.reason === 'update') |
8 injectContentScripts(); | 8 injectContentScripts(); |
9 }); | 9 }); |
10 | 10 |
(...skipping 11 matching lines...) Expand all Loading... |
22 reply(); | 22 reply(); |
23 }); | 23 }); |
24 | 24 |
25 // Inject the content scripts into every open tab, on every window. | 25 // Inject the content scripts into every open tab, on every window. |
26 function injectContentScripts() { | 26 function injectContentScripts() { |
27 var scripts = chrome.runtime.getManifest().content_scripts[0].js; | 27 var scripts = chrome.runtime.getManifest().content_scripts[0].js; |
28 | 28 |
29 chrome.tabs.query({}, function(tabs) { | 29 chrome.tabs.query({}, function(tabs) { |
30 tabs.forEach(function(tab) { | 30 tabs.forEach(function(tab) { |
31 scripts.forEach(function(script) { | 31 scripts.forEach(function(script) { |
32 // This will produce an error if extensions are prohibited on the | 32 chrome.tabs.executeScript( |
33 // tab (e.g., chrome:// pages), but we can ignore it. | 33 tab.id, |
34 chrome.tabs.executeScript(tab.id, | 34 { |
35 { | 35 file: script, |
36 file: script, | 36 allFrames: true, |
37 allFrames: true, | 37 runAt: 'document_start' |
38 runAt: 'document_start' | 38 }, |
39 }); | 39 function() { |
| 40 if (chrome.runtime.lastError) { |
| 41 // An error will occur if extensions are prohibited on the |
| 42 // tab (e.g., chrome:// pages). We don't need to do anything |
| 43 // about that, but we do need to catch it here to avoid an |
| 44 // error message on the Extensions page. |
| 45 var message = chrome.runtime.lastError.message; |
| 46 if (message != 'Cannot access a chrome:// URL' && |
| 47 message != 'The extensions gallery cannot be scripted.' && |
| 48 !message.startsWith('Cannot access contents of the page.')) { |
| 49 throw message; |
| 50 } |
| 51 } |
| 52 }); |
40 }); | 53 }); |
41 }); | 54 }); |
42 }); | 55 }); |
43 } | 56 } |
OLD | NEW |