0

ok so i'll do a big refresh:

this is my manifest:

{
"manifest_version": 2,
"name": "OnlineShoppingManager",
"version": "1.0",
"description": "The extension shows you how much you spend on online shopping",
"icons": {
    "128": "icon128.png",
    "48": "icon48.png",
    "16": "icon16.png"
    },
"browser_action":{
    "default_icon": "icon16.png",
    "default_popup": "popup.html"
},

"options_page" : "options.html",

"background":{
    "scripts":["eventPage.js"],
    "persistent": false 

},
"permissions": [
    "storage",
    "notifications",
    "contextMenus"
]

}
my full js: short explanation: in this part im init an empty array.

$(function(){
var boole=0;
var b=0;
    $('#enter').click(function(){

        if (boole==0){
        $("#enterS").show();
        boole=1;
        b=1;
        }
        if(b==1)
        {var purchases = [];
            chrome.storage.sync.set({purchases:[]});}
        else{
            $("#enterS").hide();
            boole=0;    
            $('#Amount').val('');
            $('#ProductName').val('');
            $('#Date').val('');
            }       

});

}); short explanation:in this part geting the array from the storage, and show this to the user in a table, this goes right ahead, b4 the user do something. and in the other part after the click event, im adding my user new data to the table, and adding it to the 2D array (the form has 4 inputs, each form field is saved as a row in the array)

    chrome.storage.sync.get({purchases:[]},function(arr)
{
    var purchases=arr.purchases;
    var l=purchases.length();
    var table = document.getElementById("MyTable");


    for(var i=0;i<l;i++)
    {
        var tr = document.createElement("tr");
        for(var j=0;j<4;j++)
        {
            var txt = document.createTextNode("some value");
            var td = document.createElement("td");
            td.appendChild(txt);


        }
        tr.appendChild(td);
        table.appendChild(tr);

    }       
});

$('#submitForm').click(function(){

    chrome.storage.sync.get({purchases:[]},function(arr)
    {
    var row=[$('#ProductName').val(),$('#Amount').val(),$('#Date').val(),$('#WebStore').val()]; 
    var purchases=arr.purchases;
    purchases.push(row);
    //$('#product').text(purchases[0][3]);
    var table = document.getElementById("MyTable");
    var tr = document.createElement("tr");


    for(var j=0;j<4;j++)
        {
            var txt = document.createTextNode(purchases[0][j]);
            var td = document.createElement("td");
            td.appendChild(txt);

            tr.appendChild(td);
        }       
    table.appendChild(tr);
chrome.storage.sync.set({purchases:purchases});
});

html relevant part:

<form id="enterS" style="display: none;" method="post" >
    <label for="ProductName">Product Name:</label>
    <input type="text" id="ProductName">

    <label for="Amount">Amount:</label>
    <input type="text" id="Amount">
    <br>
    <label for="Date">Date:</label>
    <input type="text" id="Date">
    <br>
    <label for="WebStore">Which Web?:</label>
    <br>
    <input type="text" id="WebStore">
    <br>
    <input type="button" value="Submit" id="submitForm">
    <input type="button" value="Reset" id="reset">

    </form>


    <h3>the product name is..<span id="product">0</span></h3>

    <button class="button" id="show" style="vertical-align:middle"><span>Show My Spendings </span></button>
    <div id="showInfo" style="display: none;">
    <h3>total is:<span id="total">0</span></h3>
    <table id=MyTable>
    <tr>
    <th>Product</th>
    <th>Amount</th>
    <th>Date</th>
    <th>Site</th>
    </tr>
    </table>

i dont have a background file yet.

so ill try to make my self clearer this time. after some helper said it i understood that the problem is the asynchronous of the set,get functions, so i looked over the answers for this problem (just understanding what was the problem was helpful!) and i understood that i have a lack of knowledge in few things.

and i saw alot of people write in this form (this is an exmple):

function storeUserPrefs() {
var key='myKey', testPrefs = {'val': 10};
    chrome.storage.sync.set({key: testPrefs}, function() {console.log('Saved', key, testPrefs);});

}

andi have a problem to understand , he is trying to set testprefs, why should he use the key,and how would i do in my case when i want to an array.

and a stupied question, to see if im starting to understand:my purpose is to "wait" to set func to finsh her work and the continue with the ret of the code? and how the console.log help me to do this?

im sorry im just so confused...

i have great hopes that ill be clearer this time, please help if you can, and forgive my ignorance, and if i didnt write it good this time ill, edit again.

thx!

albert1905
  • 147
  • 1
  • 3
  • 12
  • Please [edit] the question to be on-topic: include a **complete** [mcve] that duplicates the problem. Usually, 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 Oct 18 '16 at 15:43
  • We need the actual code you are using for this process, not just bits and pieces broken up in order not to be guessing as to what your problem is. – Makyen Oct 18 '16 at 15:45
  • Based on the incomplete code you have provided: It appears that you are using [`storage.sync.set()`](https://developer.chrome.com/extensions/storage#method-StorageArea-set) and then immediately trying to get that information. The `storage.sync.set()` method is asynchronous. The data is not immediately available. It is only available in the callback function, or after the callback has been executed. – Makyen Oct 18 '16 at 15:50
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Makyen Oct 18 '16 at 15:50
  • im sorry i thought the opposite, that the entire code will exhaust you. i will check the possibility above, and if i wont resolve it, i willedit my post and ask your help again. thx for all your help!!! – albert1905 Oct 18 '16 at 18:40
  • We don't need all your code, just enough to duplicate the problem. The point about a **[mcve]**, is that it is a *minimal*, but *complete*, example that duplicates (verifies) the problem. Creating a [mcve] may involve you creating a completely different extension which just demonstrates the problem. We should be able to take the code provided in the question and, without adding anything, be able to duplicate the problem. Sometimes, in the process of creating a [mcve], you will find the solution to your own problem. – Makyen Oct 18 '16 at 18:53
  • The reason that a [mcve] is required is that we want to help. It is **much** easier to help if we don't have to recreate all the code needed to duplicate the problem. This is code that you already have. So, please help us to help you and provide a *complete* [mcve] that duplicates the problem with such questions. Without a [mcve] the amount of effort required to even begin to help you is **much** higher which *significantly* reduces the number of people willing/able to help you. Even if we put out the extra effort, we have to *guess* at significant portions of what your problem might be. – Makyen Oct 18 '16 at 18:54
  • Use [Storage Area Explorer](https://chrome.google.com/webstore/detail/storage-area-explorer/ocfjjjjhkpapocigimmppepjgfdecjkb) or any other manual method (console.log) to ensure the data is actually written. – wOxxOm Oct 19 '16 at 04:26
  • Makyen, Hi i chacged my post completly i would be so greatful if ill get your help! – albert1905 Oct 19 '16 at 14:05

0 Answers0