Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
3.11/5 (2 votes)
See more:
I am trying to create a extension for google chrome to add a image for each link in a search result something like WOT extension "web of trust" which shows an icon in this manner.

Basically when the user searches for something and the results are displayed the icon will be on the end or beginning of each link.

Any explanation or knowledge on this topic is greatly appreciated to help set me off in the right direction.

This is what i am trying without success.

C#
Manifest.Json
{
    "manifest_version": 2,
    "name": "Test_Extension",
    "description": "Test",
    "version": "1.0",

    "content_scripts": [
    {
        "matches": ["*://www.google.ca/*"],
        "js":["trial.js"]
    }
    ],
    "icons": {"icon": "icon.png"}
}


Javascript File (Trial.js)

XML
var imgURL = custom_icon.src = chrome.extension.getURL("icon.png");
document.getElementById("<h3 class="r">").src = imgURL;



I believe the problem is with the trial.js getElementbyID statement but im not sure as I am new to extension creation.
Posted
Updated 7-May-15 15:02pm
v4

1 solution

That is not a proper ID for an HTML element.

To select an h3 tag with the class "r" which seems to be what you are trying to do you should use document.getElementsByTagName() and loop through the results to find the one with the class "r", or document.getElementsByClassName() and loop through to find the one with the tag name "h3".

A much simpler and efficient solution would be to add jquery to your content scripts like:

JavaScript
"content_scripts": [
{
    "matches": ["*://www.google.ca/*"],
    "js":["jquery.js", "trial.js"]
}


Then you can set the "src" attribute of your element like this:

JavaScript
$('h3.r').attr('src', imgURL);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900