Could a google chrome extension be dormant yet invokable? - javascript

I am planning to develop a google chrome extension which will help with a home brew functional testing framework. Here's the puzzle:
There is no predefined url pattern where test pages could reside.
I don't want the extension to get invoked by a match-all url pattern and then decide the page is of no interest.
What I want instead is for the extension to be completely dormant and to be able to invoke it from within the test pages.
Is this possible? If so,
What should I have in my manifest?
How do I implement the wake-up call in my test page javascript?
Could the call be made whilst I am developing the extension, before it gets an id?
Edit:
This answer shows how a page could call an extension, but it needs an extension ID. Is there a way to get a temporary extension ID before the extension is published?

This is the first option I was thinking of but you do have to specify a second-level domain in the URL pattern.
Add this to your manifest.json
"externally_connectable": {
"matches": ["://.example.com/*"]
}
As such: *://*.example.com/*
not *://*.com
More information about chrome extension message passing here:
https://developer.chrome.com/extensions/messaging#external-webpage
You will need to specify an id for the communication. You can get your extensions id by visiting chrome://extensions
Not knowing your stack or build process I would set up a separate dev and prod config file with my dev and prod id's in the respective files. This will help avoid pointing to your development extension when you release your app to production.
An alternate option would be to inject on each page and invoke your script depending on the pages content.

Related

How to intercept other extensions' requests using Chrome extension API?

My code is like this, but can't intercept requests from other extensions (e.g. Postman):
chrome.webRequest.onBeforeRequest.addListener(
function(details){
console.log(details.method + " ====== "+ details.url + " ====== " + details.timeStamp);
console.log("---requestbody----: " + details.requestBody);
},
{urls: ["<all_urls>"]},
["blocking"]
);
Indeed, this code won't.
This is a security feature: webRequest cannot intercept any requests from other extensions or Chrome Apps. Otherwise, it would be possible to inject your code if another extension was loading a third-party library.
It used to be able to, but it was deemed a security bug and fixed.
There is no way to override this.
Generally, extensions are not allowed to interfere with each other (except for external messaging, but both parties have to actively participate) because of privilege escalation concerns.
You cannot intercept other extension requests but what you can do is get and then change the extension that you want to intercept and add external messaging between those two extensions.
Getting the source and modifying it
Install the Chrome extension source viewer.
Go to the page in the Chrome Web Store of the extension you want to modify.
Click on the yellow CRX button, and choose Download (screenshot).
Extract the zip file.
Read the source code, and change what needs to be changed (in your particular case, I had quickly identified that you wanted to change edit config.js and change the "channel" property). Save the changes.
Or
Copy the folder of the extension you wish to modify. ( Named according to the extension ID, to find the ID of the extension, go to chrome://extensions/). Once copied, you have to remove the _metadata folder.
Using the modified version of the extension.
Visit the Chrome extension page (chrome://extensions/).
Enable Developer mode, by putting a check on the checkbox in the upper-right corner.
Click on the "Load unpacked extension" button.
Select the folder of your extension (to know which folder is correct, check whether the folder contains a file called manifest.json).
Confirm. You're done.
Unless you've made a mistake in either of these steps (including the modification of the source code), the modified extension should work as intended.
Source: How to modify an extension from the Chrome Web Store?

List all web accessible resources in a Chrome Extension

I would like to list all the images I have in a specified folder inside of my chrome extension. However, I think I am fighting web technologies on this one. Is there any env in the chrome extension that executes javascript in a "server" context, as in, could read the files in it's neighboring folders?
Here's what my web accessible resources param looks like:
"web_accessible_resources": ["*.jpg","*.JPG", "*.png", "*.PNG"]
I have background script and content script both enabled.
I've tried running an ajax query against the dir to see if it will index but also no luck.
There seems to be no direct way.
As a workaround you can set up a native app. Get the resources and directories from the manifest file and if there are any wildcards, as in your example, search the directories for them using traditional system calls.
Here's a link to how you can set up the native app and communicate with it using your extension: https://developer.chrome.com/extensions/nativeMessaging

A Chrome extension that displays a random picture from the chosen local folder for every new tab

I am new to building Chrome extensions. I want it to be able to allow the user to choose a local folder and one random picture from there should be displayed for every new tab.
As of now, I have the images in the extension folder and have hardcoded the image names for access.
I read the following SO questions around this:
Open (Import) file in a chrome extension
Access Local Files using a Google Chrome Extension
But I am not sure which one to implement and if they are the way to go.
It's not possible* with an extension to maintain a persistent access to a folder in a filesystem. Chrome Apps can do it, extensions cannot.
Your best bet is to allow upload of files into a virtual filesystem. But it will not allow modifying the pictures without interacting with your extension again.
Alternatively, you could integrate with some cloud provider, i.e. monitor a folder in Dropbox.
* P.S. Regarding NPAPI, yes, it's being deprecated, but there is an alternative: you can have a Native Host program that your extension talks to. However, it makes it very awkward to distribute the extension - Native Host can't be submitted to Chrome Web Store. But in principle that can give you the full power of a native app.

How can I launch a Chrome Packaged App through javascript?

I want to be able to launch my packaged chrome app via javascript either on-page or through an extension. Am I able to do this? I have done a fair amount of research with no answer in either direction. Can somebody at least point me in the right direction?
chrome.management.launchApp can be used to launch an app in an extension.
The API reference is available here: https://developer.chrome.com/extensions/management.html#method-launchApp.
You can make certain resources in your extension available, and then you should be able to "window.location" to that.
The URL scheme is chrome-extension://[PACKAGE ID]/[PATH].
Example:
//This is **not** in your packaged app, but in another web page
window.location = "chrome-extension://abdecbedphjijkaed/index.html";
In your packaged app, you'll need to declare which resources can be reached via a url in your manifest:
"web_accessible_resources": [
"images/my-awesome-image1.png",
"images/my-amazing-icon1.png",
"index.html"
]
See more: https://developer.chrome.com/extensions/manifest.html#web_accessible_resources
NOTE: This might not work. While the user can use "chrome://" urls, I'm not sure if web pages can
This feature is coming. See Issue 111422: Add ability for apps to register for URL handling.

firefox addon: Talking back to the script

I am new to extension programming but find making extensions in Chrome much more easier, but now that I am trying to port my test extensions to FF I have a few questions of how to do the same things I do in Chrome... now in FF.
For example:
In Chrome I have a page in my extensions directory called: domains.html
I link to that page from my popup and it has access to all my scripts etc but the "outside" world cannot directly access that.
Is there any such thing in FF so that I can show a page that is in my add on folder and has access to my add-on's JS and other files?
Thanks!
Take a look at some of the docs for opening URLs in new Tabs and manipulating the content of the tab:
https://developer.mozilla.org/en/Code_snippets/Tabbed_browser#Opening_a_URL_in_a_new_tab
Once you get comfortable with that, try opening an HTML page that lives in your add-on. First, you need to be aware of the 'content' package name you registered in your chrome.manifest file. Second, make sure your 'domains.html' file is in the content folder of your add-on. To open the web page in a new tab using a button or menu in Firefox use code like this:
gBrowser.addTab("chrome://mypackagename/content/domains.html");
You can should be able to load other JS and CSS files from your add-on into the web page using similar chrome:// URIs.

Categories