开发者

how to check if my javascript browser extension works

I am trying to create a simple javascript based extension for Google Chrome that takes data from one specific iframe and sends it as part of a POST request to a webpage. The web page that sends the data submitted by POST request, to my email address.

I tried running the extension, it looks to be running fine, but I am not getting any email. The servlet which receives form data is very simple, I dont think there is any error in it. What I want is some way to check if the javascript based extension works or not.

The javascript code is given below-

var mypostrequest=new ajaxRequest()
mypostrequest.onreadystatechange=function(){
    if (mypostrequest.readyState==4){
        if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
            document.getElementById("result").innerHTML=mypostrequest.responseText
        }
     开发者_高级运维   else{
            alert("An error has occured making the request")
        }
    }
}
var namevalue=encodeURIComponent("Arvind")
var descvalue=encodeURIComponent(window.frames['test_iframe'].document.body.innerHTML)
var emailvalue=encodeURIComponent("arvindikchari@yahoo.com")
var parameters="name="+namevalue+"&description="+descvalue &email="+emailvalue
mypostrequest.open("POST", "http://taurusarticlesubmitter.appspot.com/sampleform", true)
mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
mypostrequest.send(parameters)

UPDATE

I made changes so that the content in js file is invoked by background page, but even now the extension is not working.

I put the following code in background.html:

<script> 
    // Called when the user clicks on the browser action.   
    chrome.browserAction.onClicked.addListener(function(tab) { 
        chrome.tabs.executeScript( null, {file: "content.js"}); 
    }); 
    chrome.browserAction.setBadgeBackgroundColor({color:[0, 200, 0, 100]}); 
</script>


Looking at your code looks like you are trying to send cross domain ajax request from a content script. This is not allowed, you can do that only from background pages and after corresponding domains are declared in the manifest. More info here.

To check if your extension works, you can open dev tools and check if there any errors in the console. Open "Network" tab and see if request was sent to your URL. Place console.log in various places in your code for debugging, or use full featured built in javascript debugger for step-by-step debugging.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜