How can I use Tippy.js in a Google Chrome content script? - google-chrome-extension

I would like to use Tippy.js in a simple Chrome Extension I am building. Basically, I want to use Tippy alongside my content script, but I do not know how to include it without using the cdn.
I know I should include it in the manifest.json file with the content_scripts, but you are not supposed to use a cdn link here. If I install the package with node, I get all the files found here: https://unpkg.com/browse/tippy.js#4.3.5/ but I'm not sure which one to link in the manifest file.
Here is what I currently have in my manifest.json:
"content_scripts": [
{
"matches":[
"<all_urls>"
],
"js": [
"./node_modules/tippy.js/umd/index.min.js",
"./src/content.js"]
}
],
I realize this is probably a silly attempt at including the external library, but I'm just not sure how to include libraries like this that don't come packaged in a single file.

For tippy.js you need popper.js as well
Save this two files in your project
https://unpkg.com/popper.js#1.15.0/dist/umd/popper.min.js
https://unpkg.com/tippy.js#4.3.5/umd/index.all.min.js
Add this two files in the content script, like you have added

Umd is a choice in this case, because Chrome Extension didn't support import and export keywords, so choose node_modules/tippy.js/umd/index.min.js and make sure that node_modules is at the same directory as your manifest.json file.
"content_scripts": [
{
"matches":[
"<all_urls>"
],
"js": [
"node_modules/tippy.js/umd/index.min.js",
"src/content.js"
],
"css": [
"node_modules/tippy.js/themes/light.css"
]
}
]

Related

How to run a content script inside a chrome-extension?

We have a separate html page that can be access inside chrome-extension:// and we want to run the content script inside that extension page, but it seems like the content script doesn't run inside it.
Even updated the manifest.json file to add chrome-extension domain but to no avail?
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*", "file://*/*.pdf", "chrome-extension://*/*"],
"js": ["contentPage.js", "polyfills.js", "runtime.js", "main.js"],
"css": ["styles.css"],
"run_at": "document_idle"
}
],
Note that we should only be running it inside our own content script, so i will also change the chrome-extension://*/* to chrome-extension://{chromeId}/*.
What should be the approach for this one?

Renaming content_script stops it from running in Chrome Extension

In my manifest.json I have:
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["js/punycode.js", "js/content-main.js"]
}
],
However if I rename the content-main.js to content-main1.js update the manifest.json to reflect the change ("js/content-main1.js") and reload the extension the script no longer runs.
How is this possible? Thanks
The Reload Extensions extension that I was using wasn't reflecting the changes in the directory.

Chrome Content script injection in MHTML files

It seems the Chrome does not inject content script into local files that are of type MHTML. They might do this for security reason. They don't allow you to download files that have MHTML extension either. So that makes me suspicious.
My content script gets injected properly if the local file type is HTML.
Here is my manifest:
"content_scripts": [
{
"matches": [ "http://*/*", "https://*/*", "file://*/*", "<all_urls>" ],
"run_at": "document_start",
"js": [
"js/contentscript.js"
]
}
],
"permissions": [ "tabs", "http://*/*", "https://*/*"],
In the extension management page I also checked:
[x] Allow Access to file URLs
And finally the error I get:
test1.mhtml:1 Blocked script execution in 'file:///Users/test/Downloads/test1.mhtml'
because the document's frame is sandboxed and the 'allow-scripts' permission is not set.
Is there anyway to work around this and get my script injected in .mhtl file?
Update:
Here is a simple test extension that shows script injection and a test mhtml file. Make sure you check this check box in extension management page. [x] Allow Access to file URLs
Update 2:
Found it. It is a chrome bug https://code.google.com/p/chromium/issues/detail?id=452901
Update 3:
so it looks like that it works but chrome debugger just does not show the content script files in the UI when the file type is MHTML.
The content script is added via the standard declaration, for example:
"content_scripts": [
{
"matches": [ "<all_urls>" ],
"run_at": "document_end",
"js": ["content.js"]
}
],
Notes:
it won't be shown in Chrome devtools -> Sources -> Content scripts panel which seems a bug.
MHTML has some restrictions so certain features may not work, use console.log everywhere.

chome.tabs.executeScript from external file? What permissions do I need to change in manifest.json?

I'd like to run chrome.tabs.executeScript to run a file stored on a remote server (for testing purposes, that server is localhost). Here is the code I have so far:
chrome.tabs.executeScript(tabId, {file: 'http://localhost/js/myTestFile.js'}, function() {
//Do some stuff on completion
});
I know that by default, programmatically injected content scripts cannot be sourced from a remote location, but that you can "whitelist" certain sources in the manifest to change that. Here is my manifest at the moment:
//Extensions Permissions
"permissions": [
"activeTab",
"notifications",
],
//External access permissions
"externally_connectable": {
"matches": [
"http://localhost/*",
]
}
//Directories available to the extension
"web_accessible_resources": [
"js/*",
"html/*",
"img/*"
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["js/lib/require-2.1.8.min.js", "js/extension/contentManager.js"],
"all_frames": true
}
],
In what way can I modify the manifest to allow a remote JS file to be injected as a content script?
You need to ask for "tabs" permissions to inject script into a page. Note that you can inject code or a link to the script. There may be an issue injecting a remote script, but you can pull down the JS and inject that directly into the page.
To include a remote script into the background page, you need to list the domain in content_security_policy and it must be https unless it is on localhost.

An URL used to inject a content script into web pages in Chrome extension

In the manifest file of the Chrome extension "Search by Image", which allows you to search images on Google by just right clicking pictures on the web, the "content_scripts" field looks like this:
"content_scripts": [ {
"css": [ "quimby.css" ],
"js": [ "jquery-1.7.1.min.js", "quimby.js" ],
"matches": [ "\u003Call_urls\u003E" ]
} ]
How does the match pattern "\u003Call_urls\u003E" work?
\u003C and \u003E are unicode characters. What it is matching is <all_urls>
Those files will be injected into every page that the extension has permission to modify.

Resources