Writing a simple chrome extension - google-chrome

I'm trying to write a simple Chrome extension. I learnt how to write the "hello world" extension that Google put out, but how do I write an extension that will actually do something? Nothing online seems to explain it well. I know it involves HTML and CSS(? ), but what do I do? Say I want to write an extension that will enlarge everything--do I write an HTML file that does this and stick it in my extension folder? And what do I write in the manifest.json file so that it actually uses the HTML file?
Also, I'm a total beginner (I don't know HTML, all I know is a bit of Java) if you couldn't tell from the basic-ness of my questions.

In order to interact with the DOM of the site the user is viewing once the extension is installed you need to use content scripts. Look at the added lines in the manifest here:
http://code.google.com/chrome/extensions/content_scripts.html
Those lines let you indicate what js and css you want to use for the site and also specify what specific domains with 'matches':
"content_scripts": [
{
"matches": ["http://www.google.com/*"],
"css": ["mystyles.css"],
"js": ["jquery.js", "myscript.js"]
}
],

Files needed:
icon.png ( http://www.mediafire.com/imageview.php?quickkey=37phkkdmd1dv1zf&thumb=4 )
manifest.json
popup.html
Put all in a folder together.
.
in the manifest.json:
{
"name": "My First Extension",
"version": "1.0",
"description": "The first extension that I made.",
"browser_action": {
"default_icon": "icon.png"
},
"browser_action": {
"default_icon": "icon.png",
"popup": "popup.html"
}
}
put in popup.html:
hello world
Save both files.
go to extentions ( chrome://extensions/ )
And click developer mode. http://snpr.cm/HBuhYF.jpg
Click "load unpacked extention". and locate the folder.
It should load fine.
Click on the new extention and see the glory :)
edit, just read your post - you learned a hello world /facepalm

Related

JSON - Make popup navigate to Google

I'm trying to write a Chrome extension where, when clicked, it will drop down Google. For now, it only loads a local .html file. Here is the code I currently have:
{
"manifest_version": 2,
"name": "theName",
"description": "TheDesc",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "http://www.google.com",
"default_title": "Tooltip"
},
"permissions": [
"activeTab",
"https://ajax.googleapis.com/"
]
}
I have 4 files in my folder: icon.png, manifest.json, popup.html, & popup.js. I am using the "Getting Started" tutorial from https://developer.chrome.com/extensions/getstarted as a template, as I am extremely new to all of this (vb6 programmer for years).
Edit:
I finally got it to open a webpage in the little popup. Now, I need to grab the Title of the current tab's URL, remove the spaces from it, trim it down to 30 characters max, assign it to a string, then navigate to www.website.com/STRING. What I'm thinking is the extension needs to create a new popup.html each time, but that could be taxing on the server. Perhaps this can be done straight from the extension? I don't have a clue. Just guessing at this point.

Chrome Extensions i18n: Redirecting to locale-specific content

Question: How can I redirect users to most-relevant content, sitting in 3 different sub-directories, depending on their preferred language?
What I have
I have created 3 different _locales sub-directories, each containing language-specific messages.json files:
_locales/en/messages.json
_locales/en_GB/messages.json
_locales/de/messages.json
In manifest.json in the root directory, this already works well and as expected to correctly adapt the extension's name and description in the respective language, using:
manifest.json
{
"manifest_version": 2,
"name": "__MSG_extensionName__",
"description": "__MSG_extensionDescription__",
"version": "1.0",
"default_locale": "en",
"browser_action": {
"default_title": "__MSG_extensionName__",
"default_icon": "icon.png",
"default_popup": "popup.html"
}
}
Where I am stuck
As the user clicks on my extension's icon, I also want to load language-specific content from the most-relevant sub-directory -- instead of the "default_popup": "popup.html" line -- because some URLs that I link to from the popup.html files also differ by language:
_locales/en/popup.html
_locales/en_GB/popup.html
_locales/de/popup.html
So I thought the best way to do it is to exchange that one line from my root manifest.json that currently reads
"default_popup": "popup.html"
with a line that reads
"default_popup": "__MSG_initialContent__"
and then within each of the _locales sub-directories' messages.json files have
"initialContent": {
"message": "popup.html"
}
so that it reads the content locally from within that subdirectory.
BUT: That is where it all falls apart. Then I get a "This webpage is not found error".
I have tried, to no avail, these differing variations (examples below for _locales/de/messages.json) -- all leading to the same error message:
"initialContent": {
"message": "popup.html"
}
then
"initialContent": {
"message": "_locales/de/popup.html"
}
then
"initialContent": {
"message": "/de/popup.html"
}
and lastly
"initialContent": {
"message": "de/popup.html"
}
What am I doing wrong?
Thanks!
I know this question is very old but I just found it and I guess it would be nice for future visitors to get a solution.
What you need to achieve this are "Predefined messages"
Quote:
Predefined messages
The internationalization system provides a few predefined messages to help you localize. These include ##ui_locale, so you can detect the current UI locale, ...
[...]
The special message ##extension_id can be used in the CSS and JavaScript files, whether or not the extension or app is localized. This message doesn't work in manifest files.
The following table describes each predefined message.
##extension_id
The extension or app ID; you might use this string to construct URLs for resources inside the extension. Even unlocalized extensions can use this message.
Note: You can't use this message in a manifest file.
##ui_locale The current locale; you might use this string to construct locale-specific URLs.
[...]

could not load javascript for contentscript

i am developing extension for google chrome. i am trying to load css file.if the css file is in root directory of extension then it loaded fine. but if i put css in /css folder then try to load css then it gives error could not load javascript for contentscript
{
"name": "test",
"version": "1.0",
"manifest_version": 2,
"description": "This is test",
"background":{"scripts":["background.js"]},
"permissions":
[
"http://*/*",
"https://*/*",
"file://*"
],
"content_scripts":
[
{
"matches":["*://mail.google.com/*"],
"css":["mystyle.css", "default.css","style.css"],
"js":["jquery.js","myscript.js","DOMAlert.js","css/core.js"]
}
]
}
i am new at developing extension so please help what's wrong.
I seem to be having the same issue. I managed to achieve this when I added a script that is external to my plugin. Check all of your js scripts to have exact spelling and case. It only complains about javascript if there is a permissions issue or the file does not exist.

"exclude_matches" in manifest.json does nothing?

I'm having a problem controlling what pages my content scripts are injected into. The chrome extension developer guide specifies that I can use an "exclude_matches" directive in my manifest.json to exclude certain pages from injection.
However, this doesn't seem to have any effect. My content script still executes on pages that I have specified as ignored.
I have put the steps to reproduce in a Gist. The code is also available on Github.
Any ideas what I'm doing wrong?
manifest.json
{
"name": "Testing Extension",
"version": "1.0",
"description": "Test the chrome extensions exclude_matches.",
"content_scripts": [{
"matches": ["http://*/*", "https://*/*"],
"exclude_matches": ["http://news.ycombinator.com/"],
"js": ["content.js"]
}]
}
content.js
console.log("hello from the content script");
This is Bug #100106. exclude_matches do not function properly.
To solve the problem, use exclude_globs instead of exclude_matches.
Also, your exclude_matches rule does only match http://news.ycombinator.com/.
Suffice the pattern with an asterisk to match the whole site: http://news.ycombinator.com/*.
See also: Match patterns.

Google Chrome extension relative path

I'm developing a google chrome extension and I'm running into a relative path problem.
If I give a relative path to an image and open the plugin in a certain page it will look for that image in the website's path rather than the extension's.
Any ideas?
If you're using CSS in your extension pages (background, popup, infobar, etc) then you can use relative paths with a slash (/):
background-image:url("/sprites.png");
The above should work, everyone uses it. But, if your using it for content scripts and you can do the same for any css, you would need to use the predefined message like:
background-image:url('chrome-extension://__MSG_##extension_id__/sprites.png');
If you want to programmatically set it, you can use the chrome.extension.getURL syntax as following:
var url = chrome.extension.getURL('sprites.png');
These are the ways that you can refer to a specific url/image.
In addition, as mentioned in this answer, if you place your image assets in a directory, these files are not accessible in the web page DOM automatically. The developer should specify the resources that can be loaded the page by using the "web_accessible_resources" setting in the manifest.json file:
#mohamed's answer worked for me but it took my a while to put it all together. I've answered this else where but here is the solution that worked for me.
My solution.
With Menifest v2 you need to add web_accessible_resources to the file and then use chrome-extension://__MSG_##extension_id__/images/pattern.png as the url in your css file.
CSS:
#selector {
background: #fff url('chrome-extension://__MSG_##extension_id__/images/pattern.png');
}
Manifest.json
{
"manifest_version": 2,
"name": "My Extension Name",
"description": "My Description",
"version": "1.0",
"content_scripts": [
{
"matches": ["https://mydomain.com/*"],
"css": ["style.css"]
}
],
"permissions": [
"https://mydomain.com/"
],
"browser_action": {
"default_icon": {
"19": "images/icon19.png",
"38": "images/icon38.png"
},
"default_title": "My Extension Name"
},
"web_accessible_resources": [
"images/pattern.png"
]
}
p.s. Your manifest.json might look different to this one.
In some cases you might even use inline base64 encoding of the image. For example,
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAB..." />
Same you can apply to your CSS. You can find image encoders all over the web.