I want to detect the button event in the page,I inject my script.js strong text file from background.html, when button is click,I want to alert some message than let button do his work.
so I have two question,
1.I try to use jquery in my srcript.js but its seem not work,i have include jquery file in background.html, how can i use jquery in .js file?
2.how to detect button event when it be click?
below is my code:
background.html
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script>
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null,{file:'script.js',allFrames:true});
});
</script>
script.js
if( self.frameElement && self.frameElement.tagName == "IFRAME" ){
$(":button").bind("click",function(){
alert("iframe button");
});
}
else{
$(":button").bind("click",function(){
alert("parent button");
});
}
As to why jQuery in your script is not working, that is because you have included jQuery in your
background page, you need to load it into the user’s page’s extension’s world. To make it clear. Yourscript.jsis supposed to be loaded in user’s page. Chrome will load into the page but in anisolated worldwhich is your extension’s world on that page. You have to load jQuery on that page just like yourscript.js. As below:As to your other question regarding listening to button clicks, its unclear from your question which button are you speaking of. Is it the browser action button? If that is the case your code will work just fine. If it is a button on user’s DOM, you will have to add script as described above that listens to the click event and informs your extension (
background.html), yourbackgroundpage in turn will load the script.