1

I'm trying to build a chrome extension in which I have to store some data locally. Code given below is my popup.html and I can't understand why this doesn't set values in local storage. My manifest.json has "storage" and "tabs" permission.

<script type="text/javascript">
function save() {
  var username = document.getElementById('username').value;
  var password = document.getElementById('password').value;
  chrome.storage.local.set({'username': username}, function(){});
  chrome.storage.local.set({'password': password}, function(){}); 
}
</script>
Roll Number
<input type="text" name="username" id="username"></input>
Password
<input type="password" name="password" id="password"></input>
<button onclick="save()" type="submit" name="submit">Save</button>

This is my manifest.json file.

{
  "manifest_version": 2,

  "name": "Autologin",
  "description": "Blah bhla.",
  "version": "1.0",
  "permissions": [
    "http://xxxxxxxxxxxxx/*",
    "storage"
  ],
  "content_scripts": [
    {
      "matches": ["http://xxxxxxxxxxxxx/*"],
      "js": ["autologin.js"]
    }
  ],
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "autologin.html"
  }
}
  • You'll need to post your manifest. Chrome generally doesn't like to run in-html javascript; you should move that to a separate file. – Teepeemm Feb 17 '14 at 14:41
  • Edited. But in-html javascript runs in chrome. –  Feb 17 '14 at 15:53
  • You should edit your question, not just your code, because as posted, this code won't work. As Teepeemm said, manifest version 2 automatically sets CSP to disallow inline JavaScript: http://developer.chrome.com/extensions/contentSecurityPolicy.html. We'll assume you've already read the console and dealt with the error messages you're seeing about CSP. Are there other errors in the console? – sowbug Feb 17 '14 at 17:06
  • Nope. No errors in the console. –  Feb 17 '14 at 17:10
  • I think it more likely that you just aren't seeing the errors. For your content script, change the console's `page context` to your extension's id. For your browser action, right click the icon and `Inspect Popup`. Do they still show no errors? – Teepeemm Feb 20 '14 at 18:41
  • did you ever fix this issue? – Newbie Feb 28 '16 at 01:50
  • Nope. That was a hobby weekend project. Left it midway. –  Mar 02 '16 at 07:47

2 Answers2

1

Try this, It worked for me

var dataObj = {}; dataObj[username] = username; dataObj[password] = password; chrome.storage.local.set(dataObj, function() {});

Sunil Kashyap
  • 2,777
  • 2
  • 12
  • 27
1

You need to move the script part out of html by creating a popup.js and include it in your html.

In popup.js

function save() {
  var username = document.getElementById('username').value;
  var password = document.getElementById('password').value;
  chrome.storage.local.set({
   'username': username ,
   'password': password
  }, function(){});

}

document.getElementByName("submit").onclick=save;

In popup.html

Replace that bunch of <script>...</script> as <script src=popup.js></script>

In manifest.json

"default_popup": "popup.html" //the name of your popup html file
Raymond
  • 1,164
  • 8
  • 21