0

I build a Firefox Extension with an options page using this documentation Implement a settings page

On this settings page, the user has the possibility to enter Username and Password which then are stored in localStorage.

function saveOptions() {

localStorage.setItem("hbz_username", document.querySelector("#username").value);
localStorage.setItem("hbz_pass", Aes.Ctr.encrypt(document.querySelector("#password").value, '12345678910111213', 128));

}

The addon manipulates the DOM of a website and generates links to a REST Webservice within HTML elements which are containing a string that matches a specific Regex pattern. When one clicks on that link, the extension should load the user credentials from localStorage

var username= localStorage.getItem("hbz_username");
var passwd = localStorage.getItem("hbz_pass");

But this doesn't work. When I'm trying it the way described in the above listed documentation, I get a "browser is not defined" Error in the F12 Console.

function restoreOptions() {

function setCurrentChoice(result) {
username = result.username;
}

function onError(error) {
console.log(`Error: ${error}`);
}

var getting = browser.storage.local.get("hbz_username");
getting.then(setCurrentChoice, onError);
}

So my question is: How do I access the localStorage from my extension?


Edit: Added the contents of my previous answer on the post to the question.

Sorry for the wait but I had to prepare a minified version of the plugin without any cryptography or such.

package.json

{
"title": "Addon From Scratch",
  "name": "addon-from-scratch",
  "version": "0.0.1",
  "description": "A basic add-on",
  "main": "index.js",
  "author": "Chris",
  "engines": {
    "firefox": ">=38.0a1",
    "fennec": ">=38.0a1"
  },
  "license": "MIT",
  "keywords": [
    "jetpack"
  ]
}

index.js

var self = require("sdk/self");

// a dummy function, to show how tests work.
// to see how to test this function, look at test/test-index.js
function dummy(text, callback) {
  callback(text);
}

exports.dummy = dummy;

var tag = "body"
var buttons = require('sdk/ui/button/action');
var tabs = require("sdk/tabs");
var pageMod = require("sdk/page-mod");
var data = require("sdk/self").data;


var button = buttons.ActionButton({
  id: "mozilla-link",
  label: "Visit Mozilla",
  icon: {
    "16": "./icon-16.png",
    "32": "./icon-32.png",
    "64": "./icon-64.png"
  },
  onClick: handleClick
});

function handleClick(state) {
  tabs.open("login.html")
}

function onOpened() {
  console.log(`Options page opened`);
}

function onError(error) {
  console.log(`Error: ${error}`);
}

pageMod.PageMod({
  include: "*",
  contentScriptFile: data.url("modify-content.js"),
  onAttach: function(worker) {
    worker.port.emit("getElements", tag);
    worker.port.on("gotElement", function(elementContent) {
      console.log(elementContent);
    });
  }
});

modify-content.js

self.port.on("getElements", function() {


    var username= localStorage.getItem("hbz_username");
    var passwd = localStorage.getItem("hbz_pass");
    //here is where usually the DOM Manipulation takes place and the link to the REST Service is created. The link contains the username and the encrypted password as param
    alert(username + passwd);


});

options.js

function saveOptions() {

    localStorage.setItem("hbz_username", document.querySelector("#username").value);
    localStorage.setItem("hbz_pass", document.querySelector("#password").value);

}

function restoreOptions() {

  function setCurrentChoice() {
    document.querySelector("#username").value = localStorage.getItem("hbz_username");
    document.querySelector("#password").value = localStorage.getItem("hbz_pass");
  }

  function onError(error) {
    console.log(`Error: ${error}`);
  }

  setCurrentChoice();
}

document.addEventListener("DOMContentLoaded", restoreOptions);
document.querySelector("form").addEventListener("submit", saveOptions);

login.html

<form>
Username:<input type="text" id="username" />
Password:<input type="password" id="password" />
<input type="submit" Value="Submit" />
</form> 
<script src="options.js"></script>

As I mentioned, I already tried the solution from the above mentioned documentation. This is the code as it is actually. I hope this helps.

Makyen
  • 31,849
  • 12
  • 86
  • 121
drummercrm
  • 77
  • 2
  • 11
  • Please [edit] the question to be on-topic: include a **complete** [mcve] that duplicates the problem. Including a *manifest.json*, some of the background *and* content scripts. Questions seeking debugging help ("**why isn't this code working?**") must include: ►the desired behavior, ►a specific problem or error *and* ►the shortest code necessary to reproduce it **in the question itself**. Questions without a clear problem statement are not useful to other readers. See: "**How to create a [mcve]**", [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), and [ask]. – Makyen Nov 24 '16 at 11:18
  • To be able to tell why you are getting the "browser is not defined" error, we are going to need more information. Basically, we are going to need to be able to duplicate the problem. Thus, my above request for a *complete* [mcve] which is sufficient to duplicate the problem. – Makyen Nov 24 '16 at 11:39
  • Based on the information in the answer you provided (which looks like it really should be an [edit] to the question), the documentation page you linked is about WebExtensions while you are writing an Add-on SDK based add-on. This is the reason that you got the "browser not defined" error. See the [Introduction to Firefox add-ons in documentation](http://stackoverflow.com/documentation/firefox-addon/3235/introduction-to-firefox-add-ons/13574/introduction#t=201609290133319078047) for a brief introduction to the different types of Firefox add-ons. – Makyen Nov 24 '16 at 19:31
  • So, I copied the code from my previous answer to my original post. But at least I got the problem, that I can't store any data with my plugin. I need to store the credentials somehow that I can access them even when the browser was closed. Is there any possibility to do this with the existing code? – drummercrm Nov 28 '16 at 08:31
  • Given that you are writing a Add-on SDK based extension, there are multiple ways that you could store this data. What you choose will depend significantly on other factors. Are you only ever going to store a single user name and password? Multiple user names and passwords? A more complex storage system, a simple one? Inside Firefox? In a separate file? in a separate database? These questions do not even begin to get into the issue of storing the username and password securely (e.g. encrypted, with the user having to enter a password to unlock the username/password combination). – Makyen Nov 28 '16 at 08:45
  • I just want to store a single username and password combination. When I call the options page and enter different credentials, the previous entered credentials should be overwritten. Algorithms to encrypt and decrypt this data are already existing. I would prefer a simple storage system (maybe in a cookie or such) without any Database or extra file. The complete solution should run inside Firefox. – drummercrm Nov 28 '16 at 08:55

0 Answers0