I am trying to create a simple clipboard extension. I have 6 textarea
objects that are used as copy/paste platforms. I want these objects to 'remember the text' for future uses (even after the extension is closed).
I this is how the textarea
object was built like:
<table>
<tr>
<td>
<textarea id="container_1" class="container" rows="4" cols="50" value="" focusout="SetTextBoxString(1)"></textarea>
</td>
<td>
<textarea id="container_2" class="container" rows="4" cols="50" value="" focusout="SetTextBoxString(2)"></textarea>
</td>
</tr>
(...)
notice that the id is container + the number of the object (goes on until 6)
in the JavaScript side, I use the onload
event to loop through all of these objects and get from the data from the global storage and put it in the textarea
s. For the input, I use the SetTextBoxString()
object to "send" whats in that field to the global storage. here is the js:
var texts = [];
Load_func()
{
for (var i = 1; i <= 6; i++) {
chrome.storage.sync.get(["Txt_" + index], function (items) {
var obj = JSON.parse(items);
var txt = "Txt_" + i
texts.push(obj.txt)
document.getElementById("container_" + i).innerHTML = texts[i]
});
if (!texts[i])
{
document.getElementById("container_" + i).value = "add"
}
}
}
SetTextBoxString(index)
{
var sorcestring = "Txt_" + index;
chrome.storage.sync.set({ sorcestring : document.getElementById("container_" + index).innerHTML }, function () {
});
}
It's working fine, but when I refresh the page the text disappears. What have I done wrong?
I know it's a long question, I have done everything I can to make it shorter, no luck. I really appreciate if you will help me. thank you in advance!
sources: