Using Dart in Chrome extension content script does not run? - google-chrome-extension

I am trying to write a Chrome extension using Dart. So far everything goes well except for the content script --- the "main" function in the content script dart file does not seem to run.
To be more specific, first of all Dartium cannot be used since giving a dart file in the "js" rule in the manifest caused Dartium to complain; I next tried to compile the dart file (with csp: true) then make the manifest to include the compiled js file directly --- then I'm stuck, it seems that no matter what I try, the (compiled) "main" function just does not run.
Any suggestions?
Update:
The manifest file:
{
"manifest_version": 2,
"name": "Assistant",
"description": "Assists you with various tasks.",
"version": "1.0",
"minimum_chrome_version": "26.0",
"permissions": ["<all_urls>", "storage"],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": [
"packages/browser/dart.js",
"dart_content_script.dart.js"
],
"run_at": "document_start",
"all_frames": false
}
],
"browser_action": {
"default_popup": "popup.html",
"default_icon": "bulb.png"
},
"background": {
"page": "background.html"
}
}
The content script dart file:
void main() {
print('main done');
}
The pubspec.yaml:
name: AssistentExtension
dependencies:
browser: any
chrome: any
dev_dependencies:
unittest: '>=0.10.0'
transformers:
- $dart2js:
csp: true
In the Chrome developer console, I can find the string "main done" meaning that the "main" function is indeed included in the compiled js, but nothing is printed meaning it is not run.

I had the same problem like yours. At that time I looked into the console logs, a log saying chrome package is missing (but i don't know what cause it), so I manually added it back to build folder, then it worked so I can see my logs written in main().
When I run test_ext that come with official chrome pub, I get a different error message in console log, again I solved it and then the test_ext sample is running well too.
So, my advise is take a look at the console log, it might help. You can open console for extension by right click on popup UI of extension and select 'Inspect Element' to open it.

Related

Content script pattern "*://mail.google.com/*" doesn't match gmail when using firefox

I have an extension that I've written for chrome. I recently decided I wanted to port it over to firefox, and I was surprised on how many things worked out of the box with no changes at all. However, one thing that has tripped me up is that for some reason my gmail.js content script is not being loaded. This is my content script:
{
"name": "Copy Machine",
"description": "Copies text more better",
"version": "0.6",
"permissions": ["contextMenus", "tabs"],
"icons": {
"16": "icon_16.png",
"32": "icon_32.png"
},
"background": {
"scripts": ["background.js"]
},
"manifest_version": 2,
"content_scripts": [
{
"matches": [
"*://mail.google.com/*"
],
"js": ["gmail.js"],
"run_at": "document_idle"
},
{
"matches": [
"<all_urls>"
],
"js": ["generic.js"],
"run_at": "document_idle"
}
]
}
The first line of my gmail.js content script is:
console.log("GMAIL SCRIPT IS RUNNING");
And the first line of my generic.js content script is:
console.log("GENERIC SCRIPT IS RUNNING");
When I open my gmail account in firefox, in the console I see "GENERIC SCRIPT IS RUNNING", but not "GMAIL SCRIPT IS RUNNING."
With google chrome, this works as I would expect, but for some reason the URL pattern isn't matching. Is there something I am missing? Thanks.
So I ended up adding "*://mail.google.com/*" to the permissions in the manifest file, and that got things working the way I expected. I then had to fix a few other bugs with apis that were not available, this is what I should have started with:
https://extensionworkshop.com/documentation/develop/porting-a-google-chrome-extension/
I then went back to file a bug report, and when I remove "*://mail.google.com/*" it's continues to work. So maybe there's some weird circular dependency thing going on? I'm not sure.

Why is my content script file loaded as an empty file?

I'm creating a Chrome extension which inserts a content script, but the content script appears to be loaded as an empty file.
manifest.json
{
"manifest_version": 2,
"name": "Test",
"content_scripts": [
{
"matches": ["http://stackoverflow.com/*"],
"js": ["content_script.js"]
}
]
}
content_script.js
console.log('hello world');
When I go to chrome-extension://<extension's id>/content_script.js, I see the correct content of console.log('hello world');, but in the browser it appears empty:
And nothing is printed to the console. Does anyone know what can cause this?
Also, I tried adding some more files and folders. All the files and folders appear in the sources tab, but all the files are empty.
I had the same issue. In my case, the reason Chrome refused to read content.js is that the file was an alias (I was using ln -s). Copy the original file to the extension folder solved the issue for me.
Saw that too a few times on Chrome dev channel, it started a few months ago, obviously a bug.
Try restarting the browser.
Try adding debugger; in the code.
Try specifying "run_at": "document_start" (or document_end) in the content script declaration.

Chrome extension background script console.log() error

In my manifest I have this:
"background": {
"page": "background.html",
// "scripts": ["background.js"],
"persistent": true
},
Note that the background script is loaded from "background.html" because of some special reason I need to have that html page. This part of the manifest cannot be changed.
With this setting, the background script is working fine, but when I include a console.log("Succeed.") line, the script loads with an error, as follows:
Mysteriously, the logging actually worked and I could see it in the background.html's console.
But I could not get rid of the error, despite adding:
var console2 = chrome.extension.getBackgroundPage().consoleοΌ›
It's a relatively minor bug, as everything seems to work except there is an error message.

chrome extension- manifest version 2 _locales issues

i'm trying to update my manifest version for my extension but it gave me this problem:
default locale was specified, but _locales subtree is missing.
this is my manifest.json file, can anyone tell me where is the problem and what can i do?
{
"name": "Selected Text",
"version": "0.1",
"manifest_version": 2,
"description": "Selected Text and some changes",
"default_locale":"en",
"browser_action": {
"default_title": "S. Text",
"default_icon": "online.png",
"default_popup": "popup.html"
},
"permissions": [
"tabs",
"chrome://favicon/",
"http://*/*",
"https://*/*"
],
{
"matches": ["http://*/*"],
"js": ["selection.js"],
"run_at": "document_start",
"all_frames": true
}
}
It sounds like you are missing the required _locales directory in your extension root. Since you are specifying a default_locale, you need to provide a _locales directory, inside of which lie your internationalization options. In your case, you would need to at least have a folder titled _locales/en, inside of which you have a messages.json file, inside of which you would set your specific parameters (see here for more information).
I got the same issue.
Its "default_locate": "en",.
But then there is still an error. So just leave it out.
Please check the file hierarchy. It should be something like below,
- manifest.json
- πŸ“‚ _locales πŸ‘ˆ
- πŸ“‚ en
- messages.json
- πŸ“‚ es
- messages.json
- πŸ“‚ ...
- messages.json
Reference
chrome.i18n
manifest
messages.json
"default_locale":"en",
look at this code and think. you will get solution .

Google Chrome Content script on secure https page. Permissions are right

Sorry, found answer by myself (at the bottom of question)
I'm implementing simple google chrome addon (shows alert on android market, for now), and i'm facing with problems. What do i have:
Manifest (see below)
Icon.png
2 JS files (jquery and mine)
css file
permissions seem to be ok
Contents of each:
Manifest:
{
"name": "My first app",
"version": "1.0",
"description" : "My frist app",
"browser_action":
{
"default_icon": "icon.png"
},
"permissions":
[
"https://market.android.com/*"
],
"content_scripts":
[
{
"matches": ["https://market.android.com/*"],
"css": ["styles.css"],
"js": ["scripts.js", "jquery.js"]
}
]
}
My css file:
*
{
color:gray !important;
}
Scripts.js file:
$(document).ready(function() {
alert("!");
});
Jquery version is 1.7.1.
And now comes the sugar:
All items on android market are grayed. But no alert.
Thank you in advance, Nick.
OMG!
Just needed jquery before my script running. Sorry.
you need to load jquery in your manifest before your script

Resources