| 849 | } |
| 850 | |
| 851 | async function injectContentScriptsAndCSSIntoExistingTabs() { |
| 852 | const manifest = chrome.runtime.getManifest(); |
| 853 | const contentScriptConfig = manifest.content_scripts[0]; |
| 854 | const contentScripts = contentScriptConfig.js; |
| 855 | const cssFiles = contentScriptConfig.css; |
| 856 | |
| 857 | // The scripting.executeScript and scripting.insertCSS APIs can fail if we don't have permissions |
| 858 | // to run scripts in a given tab. Examples are: chrome:// URLs, file:// pages (if the user hasn't |
| 859 | // granted Vimium access to file URLs), and probably incognito tabs (unconfirmed). Calling these |
| 860 | // APIs on such tabs results in an error getting logged on the background page. To avoid this |
| 861 | // noise, we swallow the failures. We could instead try to determine if the tab is scriptable by |
| 862 | // checking its URL scheme before calling these APIs, but that approach has some nuance to it. |
| 863 | // This is simpler. |
| 864 | const swallowError = (_) => {}; |
| 865 | |
| 866 | const tabs = await chrome.tabs.query({ status: "complete" }); |
| 867 | for (const tab of tabs) { |
| 868 | const target = { tabId: tab.id, allFrames: true }; |
| 869 | |
| 870 | // Inject all of our content javascripts. |
| 871 | chrome.scripting.executeScript({ |
| 872 | files: contentScripts, |
| 873 | target: target, |
| 874 | }).catch(swallowError); |
| 875 | |
| 876 | // Inject our extension's CSS. |
| 877 | chrome.scripting.insertCSS({ |
| 878 | files: cssFiles, |
| 879 | target: target, |
| 880 | }).catch(swallowError); |
| 881 | |
| 882 | // Inject the user's link hint CSS. |
| 883 | chrome.scripting.insertCSS({ |
| 884 | css: Settings.get("userDefinedLinkHintCss"), |
| 885 | target: target, |
| 886 | }).catch(swallowError); |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | async function initializeExtension() { |
| 891 | await Settings.onLoaded(); |