0

Possible Duplicate:
Port error while changing chrome extension from manifest v1 to v2

I am creating a chrome extension where I store data in localStorage of the background page. I am taking the data from a content script and sending it to background page via message passing, I don't know why, but the data is not getting stored.

Here is the code snippet I have written.

background.html:

<script>
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { 
localStorage.setItem(request.username,request.password);
sendResponse(null);
});
</script> 

content-script:

document.getElementById('login_form').addEventListener('submit',
                                                        function(){
                                                            _Userid = document.getElementById('email').value;
                                                            _UserPass = document.getElementById('pass').value;
                                                           chrome.extension.sendRequest({username: _Userid, password: _UserPass}, function(response) {
                                                                                                                                                           alert('done') });

   },false);

Please Note that Message passing is working fine, as I do get the response back from , background page and an alert box also appears.

Community
  • 1
  • 1
Amogh Talpallikar
  • 12,084
  • 13
  • 79
  • 135
  • 1
    Are there any errors on console when you debug the background page? Are you sure `_Userid` is not empty? Are you sure you want to store these data as `"amogh":"sw0rdf1sh!"` (it doesn't seem normal to use variable as a key) and not as `"user":"amogh", "password":"sw0rdf1sh!"`? Do you know that localStorage on a background page can't be accessed directly from other pages? How do you check that data wasn't stored? – Konrad Dzwinel Aug 23 '12 at 09:37
  • I went to backround.html from extensions page in chrome and checked resources. I want username to be key and password to be its value. – Amogh Talpallikar Aug 23 '12 at 09:39
  • I have cheked. _Userid and _UserPass are both proper strings. – Amogh Talpallikar Aug 23 '12 at 09:53
  • I just checked, everything works fine If I use manifest version 1 and if I use manifest version 2. Things dont work. – Amogh Talpallikar Aug 23 '12 at 11:15
  • 1
    It's because sendRequest and onRequest is deprecated in manifest version 2. Try to use sendMessage and onMessage instead. – KiL Aug 23 '12 at 11:23
  • @KiL: Thanks a Lot ! Now everything becomes clearer. Unfortunately, http://developer.chrome.com/extensions/manifestVersion.html doesnt mention these changes. – Amogh Talpallikar Aug 23 '12 at 11:27
  • 1
    @KIL **Wrong**. [`*Request` were superseded and deprecated in favour of `*Message` since **Chrome 20**](http://stackoverflow.com/questions/11752341/chrome-extension-communication-between-content-script-and-background-html/11756188#11756188). There is **no relationship between manifest version and `*Request`->`*Message`**. Also, beware of incorrect documentation - http://stackoverflow.com/a/11811936/938089 – Rob W Aug 23 '12 at 12:12
  • @Rob W: My bad. I just noticed that Google deprecated *Request in their documents. I didn't know the fact that they was superseded in Chrome 20. Thank you for correcting me! – KiL Aug 23 '12 at 12:20

0 Answers0