所有这些都可以在Chrome扩展程序中使用indexedDB调用。
我不会编写HTML,因为它是简单的标记,但是,如果需要,请告诉我。
这里讨论的代码在request.onsuccess函数内部的IF语句中:
if (getQueryVariable("deleteid") != "") {
removeFilter(id); // This
};
在if语句中,我可以使用“ getQueryVariable(” deleteid“)”直接从URL获取我的ID,并将ID“ 2”硬编码到我的函数removeFilter(2)中,但是可以正常工作,使用“ var id = getQueryVariable(“ deleteid”)。toString();在removeFilter(id)中不会“”代码注释中的其他示例。
//
//The code below works, except the functions in question inside the IF statements
//
document.addEventListener("DOMContentLoaded", function (event) {
if (!window.indexedDB) {
window.alert("Something went wrong");
};
var request = indexedDB.open('filterDB', 3);
request.onupgradeneeded = function (e) {
var db = e.target.result;
if (!db.objectStoreNames.contains('filters')) {
var os = db.createObjectStore('filters', { keyPath: "id", autoIncrement: true });
//create index for name
os.createIndex('name', 'name', { unique: false });
};
};
//success
request.onsuccess = function (e) {
console.log('Success: Opened DB');
db = e.target.result;
//Show Content
showFilters();
// The function removeFilter() is only supposed to be called once and the commented functions are just examples
if (getQueryVariable("deleteid") != "") { // the URL reads popup.html?deleteid=2
var id = getQueryVariable("deleteid").toString(); // id = 2
removeFilter(id); // calls the onsuccess function, but doesnt delete anything
//removeFilter(getQueryVariable("deleteid").toString()); // same deal
//removeFilter(2); // Deletes Item with id = 2, so the function works with a hardcoded ID
//alert(getQueryVariable("deleteid").toString()); // alerts id = 2
//alert(id); // alerts id = 2
window.location.href = "popup.html";
};
};
//Error
request.onerror = function (e) {
console.log('Error, Could not open DB');
};
document.getElementById("FilterAdd").onclick = function () {
addFilter();
showFilters()
document.getElementById("filtername").value = "";
document.getElementById("tags").value = "";
};
});
// My delete function
function removeFilter(id) {
var transaction = db.transaction(['filters'], 'readwrite');
var store = transaction.objectStore("filters");
var request = store.delete(id);
//success and error callbacks
request.onsuccess = function (e) {
db = e.target.result;
console.log("Filter " + id + " deleted");
};
request.onerror = function (e) {
alert("Sorry, the filter wasnt removed");
console.log('Error ', e.target.error.name);
};
};
// Getting the id from URL function
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) { return pair[1]; }
}
return (false);
}
我对Java语言还很陌生,并且一段时间以来我一直在努力解决这个问题
很抱歉,如果这个问题已经提出,我试图寻找但没有发现任何东西。
答案 0 :(得分:0)
在摆弄了解决问题的多种方法之后,我最终发现我只需要将getQueryVariable(“ deleteid”)。toString()转换为整数,因为我的函数removeFilter()需要一个int 。这么简单地写
removeFilter(parseInt(id));
工作..是
不知道是否对任何人有帮助。 但是有..