Posts

Showing posts with the label Google Chrome Extension

Chrome Extension Content Script Re-injection After Upgrade Or Install

Answer : There's a way to allow a content script heavy extension to continue functioning after an upgrade, and to make it work immediately upon installation. Install The install method is to simply iterate through all tabs in all windows, and inject some scripts programmatically into tabs with matching URLs. Obviously, you have to do it in a background page or event page script declared in manifest.json: "background": { "scripts": ["background.js"] }, background.js: // Add a `manifest` property to the `chrome` object. chrome.manifest = chrome.app.getDetails(); var injectIntoTab = function (tab) { // You could iterate through the content scripts here var scripts = chrome.manifest.content_scripts[0].js; var i = 0, s = scripts.length; for( ; i < s; i++ ) { chrome.tabs.executeScript(tab.id, { file: scripts[i] }); } } // Get all windows chrome.windows.getAll({ populate: true }, func...

Callback Returns Undefined With Chrome.storage.sync.get

Answer : The chrome.storage API is asynchronous - it doesn't return it directly, rather passing it as an argument to the callback function. The function call itself always returns undefined . This is often used to allow other methods to run without having to wait until something responds or completes - an example of this is setTimeout (only difference is that it returns a timer value, not undefined ). For example, take this: setTimeout(function () { alert(1); }, 10000); alert(0); Because setTimeout is asynchronous, it will not stop all code until the entire function completes, rather returning initially, only calling a function when it is completed later on - this is why 0 comes up before 1. For this reason, you cannot simply do something like: // "foo" will always be undefined var foo = asyncBar(function (e) { return e; }); Generally, you should put what you want to do in your callback (the function that is called when the asynchronous function i...

Chrome Extension: Checking If Content Script Has Been Injected Or Not

Answer : is this safe enough to naively call chrome.tabs.executeScript every time the extension icon got clicked? In other words, is this idempotent? Yes, unless your content script modifies the page's DOM AND the extension is reloaded (either by reloading it via the settings page, via an update, etc.). In this scenario, your old content script will no longer run in the extension's context, so it cannot use extension APIs, nor communicate directly with your extension. is there a similar method for chrome.tabs.insertCSS ? No, there is no kind of inclusion guard for chrome.tabs.insertCSS . But inserting the same stylesheet again does not change the appearance of the page because all rules have the same CSS specificity, and the last stylesheet takes precedence in this case. But if the stylesheet is tightly coupled with your extension, then you can simply inject the script using executeScript, check whether it was injected for the first time, and if so, ins...

Chrome.webNavigation Undefined

Answer : On top of @rsanchez answer, if you call a chrome.webNavigation in the background script of your extension, but still have this error, you may also need to add the webNavigation permission in your manifest.json . "permissions": [ "webNavigation" ] Mozilla documentation | Chrome Documentation chrome.webNavigation , as most chrome.* APIs, can only be accessed from the background script of an extension, not from content scripts. Although your file is named background.js , your manifest shows that you are using it as a content script. It is right to use a content script in this case because you need to interact with the DOM. From the fragment of code you posted, it seems that you don't need to use the webNavigation API. You can simply set your content script in your manifest to run_at: document_end , and it will run as soon as the DOM is complete. Check http://developer.chrome.com/extensions/content_scripts.html

Chrome Extension Manifest 'Matches'

Answer : You need to surround the value of the content_scripts field in square brackets: "content_scripts": [ { "matches": ["http://*"], "js": ["scripts.js"] } ] (see the Chrome Docs for more info) Incidentally, using http://*/* would be a better match for all urls (see the docs), adding https://*/* if you also need to match those as well. Edit: Following your edit, the error you are getting is because of the match pattern being incorrect. If you want to match every URL, then Google has a special pattern just for this purpose: <all_urls> Sample usage: "matches": ["<all_urls>"], See this page for more info: https://developer.chrome.com/extensions/match_patterns Any match pattern should be of the following structure [scheme] :// [host][path] scheme is '*' | 'http' | 'https' | 'file' | 'ftp' host is ' ' | ' .' (any char...

Chrome.tabs Returns Undefined In Content Script

Answer : As content script has its own limitations, chrome.tabs is only available in background scripts and popup scripts. If you wanna to use chrome.tabs then pass message from content_script to background script and play with chrome.tabs . Content scripts have only limited access to Chrome APIs. This access does not include the API you are trying to use (e.g. chrome.tabs ). If you need to use that API, you will have to do so in a background script 1 . As listed in Chrome's content scripts documentation, the APIs available to a content script are [I have placed deprecated methods in strikethrough format]: extension ( getURL , inIncognitoContext , lastError , onRequest , sendRequest ) i18n runtime ( connect , getManifest , getURL , id , onConnect , onMessage , sendMessage ) storage A couple of the listed APIs are deprecated and have been for some time. Those that are deprecated have moved to different locations (also listed above): extension.onRequest ➞ ...

Chrome Says My Extension's Manifest File Is Missing Or Unreadable

Answer : Something that commonly happens is that the manifest file isn't named properly. Double check the name (and extension) and be sure that it doesn't end with .txt (for example). In order to determine this, make sure you aren't hiding file extensions: Open Windows Explorer Go to Folder and Search Options > View tab Uncheck Hide extensions for known file types Also, note that the naming of the manifest file is, in fact, case sensitive, i.e. manifest.json != MANIFEST.JSON . My problem was slightly different. By default Eclipse saved my manifest.json as an ANSI encoded text file. Solution: Open in Notepad File -> Save As select UTF-8 from the encoding drop-down in the bottom left. Save I also encountered this issue. My problem was that I renamed the folder my extension was in, so all I had to do was delete and reload the extension. Thought this might help some people out there.

Chrome Extension: Execute Background Page Only Once When Chrome Starts

Answer : What you are using is an Event Page(background_page.js). Event pages are unloaded when the browser detects that the page is not doing anything. So what's happening is that when you open a new tab, the Event page is being reloaded and starts executing from the top again. This way chrome is able to have your app use less memory and speed up the browser. If you want to fix the problem simply use persistent:true which will make sure the page "persists" indefinitely or until the user closes the browser. If you would really like to keep your app efficient with memory, you should take a look at the runtime.onSuspend method which gets called each time your Event page unloads. This way you can save stuff before the page gets unloaded so that you can resume where you left off. UPDATE: According to current documentation you simply have to delete the persistent key (no need to change it to "persistent": true ). This is an event page : { "name...

"Chrome Extension Throws CRX File Error "CRX_REQUIRD_PROOF_MISSING"

Answer : In recent versions of Chrome only CRX3 format is supported: Instructions for Repackaging Please see the following article for detailed instructions on how to repackage Chrome apps and extensions into the CRX3 format. If you use an open source library to build extensions please verify CRX3 support with that vendor. In addition you can use https://crx-checker.appspot.com to check the version of your extension and let your vendor know. If you are unable to repackage or cannot use the CRX3 format, you can enable the ExtensionAllowInsecureUpdates policy. Note that this is only a temporary workaround, all extensions must move to the CRX3 format! M76 (July 2019) By default, CRX2 will be disabled and everyone should move to CRX3. As a temporary workaround, ExtensionAllowInsecureUpdates can be used to re-enable CRX2. (from https://www.chromium.org/crx2-deprecation) Your options are: Repack the extension in CRX3 format in some way or another, f...

ACE Editor Autocomplete - Custom Strings

Answer : you need to add a completer like this var staticWordCompleter = { getCompletions: function(editor, session, pos, prefix, callback) { var wordList = ["foo", "bar", "baz"]; callback(null, wordList.map(function(word) { return { caption: word, value: word, meta: "static" }; })); } } langTools.setCompleters([staticWordCompleter]) // or editor.completers = [staticWordCompleter] If you want to persist the old keyword list and want to append a new list var staticWordCompleter = { getCompletions: function(editor, session, pos, prefix, callback) { var wordList = ["foo", "bar", "baz"]; callback(null, [...wordList.map(function(word) { return { caption: word, value: word, meta: "static" }; }), ...ses...

Chrome Extension Id - How To Find It

Answer : Use the chrome.runtime.id property from the chrome.runtime API. You get an extension ID when you upload your extension to Google Web Store. Ie. Adblock has URL https://chrome.google.com/webstore/detail/cfhdojbkjhnklbpkdaibdccddilifddb and the last part of this URL is its extension ID cfhdojbkjhnklbpkdaibdccddilifddb . If you wish to read installed extension IDs from your extension, check out the managment module. chrome.management.getAll allows to fetch information about all installed extensions. If you just need to do it one-off, navigate to chrome://extensions . Enable Developer Mode at upper right. The ID will be shown in the box for each extension. Or, if you're working on developing a userscript or extension, purposefully throw an error. Look in the javascript console, and the ID will be there, on the right side of the console, in the line describing the error. Lastly, you can look in your chrome extensions directory; it stores extensions in director...

Can I Edit A Google Chrome Theme?

Answer : A theme is a special kind of extension that changes the way the browser looks. Themes are packaged like regular extensions, but they don't contain JavaScript or HTML code. You can download any theme and modify it by editing the manifest.json file. Colors are in RGB format. To find the strings you can use within the "colors" field, look for kColor* strings in theme_service.cc. Image resources use paths relative to the root of the extension. You can override any of the images that are specified by kThemeableImages in theme_service.cc. Just remove the "IDR_" and convert the remaining characters to lowercase. Reference: https://developer.chrome.com/extensions/themes As was mentioned, you can find it in appdata folder, e.g.: C:\Users\mizer\AppData\Local\Google\Chrome\User Data\Default\Extensions\ Hint: Sort folders by date and you can easily find the one you want :) You can find it in your appdata folders. Mine was located at: C:\Users\home\AppD...