Call a function in the webpage

1,157 views
Skip to first unread message

Bissu

unread,
Jan 18, 2011, 9:28:44 PM1/18/11
to Chromium-extensions
Hello,

I did a javascript injection by manifest.json. This code have a
function that make interactions with DOM.

## Manifest

{
// Required
"name": "Chrome smarter extension.",
"version": "0.1",

// Recommended
"description": "Bla bla bla",

"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["fill.js"]
}
],

"browser_action": {
"default_icon": "icon.png",
"popup": "popup.html"
},

"permissions": [
"tabs", "http://*/*", "https://*/*"
]
}




I did a popup.html to the Browser Action like that to call the
function I inserted by manifest.

## popup.html
<html>
<head>
<script type="text/javascript">
function click() {
alert('now!');
chrome.tabs.executeScript(null,{"preenche()"});
window.close();
}
</script>
</head>
<body>
<input type="button" onclick="click();" value="preencher 0.0.1" />
</body>
</html>

However, it is not work. The popup appear, but when I click the
button, any alert with "now!" appear.

Thanks for suggestions or contributions.

Best,
Bissu.

Giovani Ceotto

unread,
Jan 19, 2011, 1:59:12 PM1/19/11
to Bissu, Chromium-extensions
Hello,
I think your problem is very simple. I`m not sure but I think you can only execute "chrome.tabs.executeScript(null,{"preenche()"});" from the background page. One option is to use "chrome.extension.getBackgroundPage().chrome.tabs.executeScript(null,{"preenche()"});". It will probably work, but if it doesn't just use this:

//Background Page
<html>
          <script>
                    function click(){
                             chrome.tabs.executeScript(null,{"preenche()"});
                    }
          </script>
</html>

// popup.html

<html>
       <head>
               <script type="text/javascript">
                       function click() {
                               chrome.extension.getBackgroundPage().click()

                               window.close();
                       }
               </script>
       </head>
       <body>
               <input type="button" onclick="click();" value="preencher 0.0.1" />
       </body>
</html>

If you are not really adding anything else to the popup you can just use an event listener in the background page and no popup:

//Background Page
<html>
          <script>
                    //React when a browser action's icon is clicked.
                    chrome.browserAction.onClicked.addListener(function(tab) {click()});

                    function click(){
                             chrome.tabs.executeScript(null,{"preenche()"});
                    }
          </script>
</html>


--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.




-- 
Giovani H Ceotto

Arne Roomann-Kurrik

unread,
Jan 19, 2011, 3:42:50 PM1/19/11
to Giovani Ceotto, Bissu, Chromium-extensions
chrome.tabs.executeScript(null,{"preenche()"}); is invalid syntax

As explained in the documentation:
http://code.google.com/chrome/extensions/tabs.html#method-executeScript

You should use:
chrome.tabs.executeScript(null,{"code" : "preenche()"});

~Arne

Bissu

unread,
Jan 19, 2011, 4:11:23 PM1/19/11
to Chromium-extensions
Don't respond yet.

And, the debug didn't show me any error in syntax. Is it not
dettected?.

Henrique
> >> To post to this group, send email to chromium-extensi...@chromium.org.
> >> To unsubscribe from this group, send email to
> >> chromium-extensions+unsubscr...@chromium.org<chromium-extensions%2Bunsubscr...@chromium.org>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.
>
> > --
> > Giovani H Ceotto
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Chromium-extensions" group.
> > To post to this group, send email to chromium-extensi...@chromium.org.
> > To unsubscribe from this group, send email to
> > chromium-extensions+unsubscr...@chromium.org<chromium-extensions%2Bunsubscr...@chromium.org>
> > .

PhistucK

unread,
Jan 21, 2011, 12:01:44 PM1/21/11
to Bissu, Chromium-extensions
Try to add -
"all_frames": true
To the content script entry in the manifest.
Also, make sure you are not trying it on the webstore\extension gallery pages, because it will not work (content scripts cannot be run there).

PhistucK



To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.

Marcelo Henrique

unread,
Jan 21, 2011, 11:10:56 PM1/21/11
to PhistucK, Chromium-extensions
Oh thanks for advice. I put one form file in localhost to test.
I added your line but nothing happens.
One doubt, the "matches" atribute is used like this: "matches": ["<all_urls>"] ?

Let's try in parts:

1. Insert a js in a webpage if the page attend some of pre-requisites.

It can be done like it in manifest:

"content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["fill.js"],
 "all_frames": true
    }
  ],

is it right?


2. With a popup in the extension, call a function in the script was injected.

It can be done like this:

<script>
function click() {
chrome.tabs.executeScript(null,{code:"alert('now!');",allFrames:true});
window.close();
}
</script>
<input id="cosmo" type="button" onclick="click();" value="preencher 0.0.3" />

if the function is called "alert()" (it is just an example).

Is it right?

thanks for patience,
Henrique
Todos os meus emails podem ter um prazo de 48horas para resposta
2011/1/21 PhistucK <phis...@gmail.com>

PhistucK

unread,
Jan 22, 2011, 5:57:07 AM1/22/11
to Marcelo Henrique, Chromium-extensions
Two problems, the first comes from the fact that I do not have the whole code here, so it might be redundant.
1. In order to use executeScript on some URL, you have to add the host of that URL (or all of the URLs, if that is what your need) into the "permissions" key.
For example -
"permissions": ["tabs", "<all_urls>"],

2. The problem seems to be the name of the function. Do not name it "click", it seems to be a reserved name or something. Rename it to clickEvent (for example) and it will fire up -
function clickEvent()
{
 document.body.style.backgroundColor = "black";
 chrome.tabs.executeScript(null,{code:"alert('now!');",allFrames:false});
}
<input id="cosmo" type="button" onclick="clickEvent();" value="preencher 0.0.3" />

PhistucK

Marcelo Henrique

unread,
Jan 22, 2011, 1:34:54 PM1/22/11
to PhistucK, Chromium-extensions
Well,

1. I think the problem is not the click, because I tested the function with a alert and it worked.
2. I tried to execute the code in the function directly in the line by taking the file propertie:

chrome.tabs.executeScript(null,{file:"fill.js"});

Alone, in a web page ir works. But, here it didn't. Is it correct: add a js file and expect it work with elements in the DOM?
Henrique
Todos os meus emails podem ter um prazo de 48horas para resposta


2011/1/22 PhistucK <phis...@gmail.com>
For example -

PhistucK

unread,
Jan 22, 2011, 2:05:59 PM1/22/11
to Marcelo Henrique, Chromium-extensions
1. If you tested it with the developer tools - yes, it does work within the developer tools - no idea why the two situations are different, but they are.
2. What do you mean by "Alone, in a web page ir works."? what works?

I attached a ZIP file of an unpacked extension that does exactly what you say that is not working.
Maybe this will clear things up.

PhistucK
BrowserActionExtension.zip
Reply all
Reply to author
Forward
0 new messages