0

I dont get enaugh mechanism of interaction component in Google Chrome Extensions. I need to store data after click on any page and save this in storage.

As I know, it should be content script that works with content page and which send event to background script to save data.

I tried this code in content.js:

window.onLoad = function() {
    alert("s");
    document.addEventListener("click", function (e) {
        alert("d");
        chrome.storage.sync.set({'key' : "Oleg"}, function (){
           alert("Ok");
        });

        chrome.storage.sync.get('key', callback(data));
    });
}

But this is silent

Darama
  • 3,130
  • 7
  • 25
  • 34
  • 1. where is callback. 2. you mention a nonexistent background. – Zig Mandel Feb 04 '17 at 10:48
  • 1
    callback(data) definitely seems a mistake: syntactically this is function invocation, not function reference required by the API. – wOxxOm Feb 04 '17 at 13:01
  • Please [edit] the question to be on-topic: include a **complete** [mcve] that *duplicates the problem*. Including a *manifest.json*, some of the background/content/popup scripts/HTML. 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 Feb 04 '17 at 21:09
  • Discounting the `callback(data)` error, this is effectively a duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](//stackoverflow.com/q/23667086) You do `storage.sync.set()` and immediately `storage.sync.get()` both are asynchronous functions. For it to work as intended the `set()` has to complete before the `get()`. There is nothing that guarantees that they will complete in that order. If you want to use the `get()`, it needs to be in the callback for the `set()`. – Makyen Feb 04 '17 at 21:13

0 Answers0