开发者

google chrome extension does not work

开发者 https://www.devze.com 2023-04-02 06:49 出处:网络
It is a simple extension that invokes Xmlhttprequest to send POST data to a form. I have also added simple message boxes at beginning/end of the js code... The code is being invoked from a background

It is a simple extension that invokes Xmlhttprequest to send POST data to a form. I have also added simple message boxes at beginning/end of the js code... The code is being invoked from a background page and correct permissions have been granted in manifest.json. However when I click on the button for this extension, nothing is happening.

Given below is the js code for the extension-

alert("Beginning of code block");
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("POST","http://taurusarticlesubmitter.appspot.com/sampleform",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("name=Arvind&description=Test description&email=arvind@taurusseo.com");
alert("End of code block");

Also, I added the following code to background.html--

<script>
  // Called when the user clicks on the browser action.
  chrome.browserAction.onClicked.addListener(function(tab) {
    chrome.tabs.executeScript(
        null, {file: "cdr.js"});
  });

  chrome.browserAction.setBadgeBackgroundColor({color:[0, 200, 0, 100]});


 </script>

Finally, 开发者_如何学Cgiven below is my manifest.json--

{
  "name": "My Second Extension",
  "version": "1.0",
  "background_page": "background.html",
  "description": "The second extension that I made.",
   "browser_action": {
        "name": "Data in iframe",
       "default_icon": "icon.png"
},  
"permissions": [ "tabs",
       "bookmarks",
           "http://*/*",
           "https://*/*",
            "unlimitedStorage"
     ]
 }


I assume that your first code block is cdr.js? Then you are not running it from the background page. Instead your background page loads a content script that tries to send a request. Content scripts run with the privileges of the page that they have been injected into. So if that page doesn't have privileges to send a request to taurusarticlesubmitter.appspot.com then the content script won't have the necessary privileges either.

If your content script needs to perform a privileged action (like sending a request to a third-party page) it should send a message to the background page and the background page will have to do it.

0

精彩评论

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