2016-04-16 87 views
1

我試圖挽救已在彈出的選擇了單選選項,但打開彈出時,有在控制檯中的錯誤:Chrome擴展形式保存錯誤

Uncaught Error: Invocation of form get(string, undefined) doesn't match definition get(optional string or array or object keys, function callback) extensions::schemaUtils::113

的manifest.json(相關):

{ 
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'", 

"page_action": { 
     "default_icon": { 
     "19": "images/icon19.png", 
     "38": "images/icon38.png" 
     }, 
     "default_title": "Scratch theme loader", 
     "default_popup": "popup.html" 
    }, 

"content_scripts": [ 
    { 
    "matches": [ 
     "https://scratch.mit.edu/*" 
    ], 
    "js": ["jquery-2.2.2.min.js", "content.js"], 
    "run_at":"document_start" 
    } 
], 

"permissions": [ 
    "tabs", 
    "storage", 
    "declarativeContent", 
    "https://scratch.mit.edu/*", 
    "https://pastebin.com/raw/*" 
] 
} 

popup.html:

<!DOCTYPE html> 
<html> 
    <head> 
    </head> 
    <body> 
     <ul id="main"> 
     <li id="top_bar"> 
      <img src = "images/S_Themes.png"> 
     </li> 
     <li> 
      <p>Choose which theme you would like to use:</p> 
      <form id="options" action=""> 
       <input type="radio" name="op" id="op1"> <div id="op1">Example 1</div> 
       <br><input type="radio" name="op" id="op2"> <div id="op2">Example 2</div> 
       <br><input type="radio" name="op" id="op3"> <div id="op3">Example 3</div> 
       <br><input type="radio" name="op" id="op4"> <div id="op2">Example 4</div> 
       <br><input type="radio" name="op" id="op5"> <div id="op3">Example 5</div> 
      </form> 
      <button type="button" id="save">Save</button> 
     </li> 
     <li> 
      <a href="#"><div class="options"><br>Add themes</div></a> 
      <a href="#"><div class="options">Options</div></a> 
     </li> 
    </ul> 
    <script type="text/javascript" src="popup.js"></script> 
    </body> 
</html> 

popup.js:

function getValue(callback) { chrome.storage.sync.get("selected", callback); } 
var selectedvar = getValue() 
console.log("Data was loaded."); 

if ((typeof selectedvar) == "number"){ 
    var element = document.getElementById("op" + selectedvar.toString()); 
    console.log("op" + selectedvar.toString()); 
    element.checked = true; 
} 

document.getElementById('save').onclick = save; 

function save() { 
    var radios = document.getElementsByName("op"); 
    var value; 

    for(var k=0;k<radios.length;k++) 
      if(radios[k].checked){ 
      value = k + 1; 
      } 

    chrome.storage.sync.set({"selected": value}, function() { 
     console.log("Data was saved."); 
    }) 
} 

PS。我無法讓jQuery在「popup.js」中工作,所以請儘可能不要使用它。

編輯:

@伊萬Nokonoko,我在popup.js改變下面的代碼,但它不保存和變量「selectedvar」,仍然是不確定的,當它加載。

function call() {console.log("Data was loaded.");} 

function getValue(callback) { chrome.storage.sync.get("selected", callback); } 
var selectedvar = getValue(call); 

console.log(selectedvar); //prints undefined, despite being previously saved. 

回答

1

你叫用的getValue的參數作爲第二個參數chrome.storage.sync.get,但你打電話的getValue不帶參數:

VAR selectedVar =的getValue()< ---沒有參數!

這會導致你的代碼,這樣做:

chrome.storage.sync.get( 「選擇」,未定義)

這就是錯誤。考慮到chrome.storage異步工作,你必須提供一個回調函數來檢索數據。例如:

chrome.storage.sync.get("selected", function (data) { 
    if (data["selected"]) selectedVar = data["selected"] //better with dot notation 
    // then do stuff with selectedVar... 
    // ... 
}); 

嘗試用這種popup.js:

function myCallbackFunction(dataStored) { 
    if (dataStored["selected"]) { //better with dot notation: dataStored.selected 
    selectedvar = dataStored["selected"]; 
    console.log("Data was loaded."); 
    } 
    if (selectedvar) { 
    var element = document.getElementById("op"+selectedvar); //JS automatically transforms number to String 
    console.log("op"+selectedvar); 
    element.checked = true; 
    } 
} 

function getValue(callback) { chrome.storage.sync.get("selected", callback); } 
getValue(myCallbackFunction); 

document.getElementById('save').onclick = save; 

function save() { 
    var radios = document.getElementsByName("op"); 
    var value; 

    for(var k=0;k<radios.length;k++) 
      if(radios[k].checked){ 
      value = k + 1; 
      break; // once you get the checked value, you can exit the loop. 
      } 

    chrome.storage.sync.set({"selected": value}, function() { 
     console.log("Data was saved."); 
    }); 
} 

您還必須刪除/修改的ID從div的HTML文件(它們與輸入ID的衝突)

+0

這樣就停止了這個錯誤,但它仍然不起作用,我編輯了這個問題。 – Melkor

+0

@Marchill至此,答案在於http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Xan

+0

謝謝!這工作完美! – Melkor