I'm trying to save some values in the LocalStorage of WebView of JavaFX. Is it supported by WebView? About html5test.com, the LocalStorage is enabled, but I don't find any solution to save data with Javascript in the LocalStorage.
Asked
Active
Viewed 813 times
1
-
AFAIK 'coockies' (which the local storage basically is) and the javafx webview (or java in general) are not a perfect combination. Look at [this question](https://stackoverflow.com/questions/45827482/access-all-the-cookies-from-a-javafx-webview) for example. Unfortunatly there are many more simular questions both related and unrelated to the javafx webview, all without a good solution or workaround. – n247s Nov 04 '18 at 20:48
2 Answers
0
you can use this:
JSObject js = (JSObject)webEngine.executeScript("localStorage");
for example, in local storage, we have refreshTime with the value 1000.
for getting
println(js.getMember("refreshTime")) // print 1000
for setting
js.setMember("refreshTime",2000)
JSObject from import netscape.javascript.JSObject;

Rasoul Miri
- 11,234
- 1
- 68
- 78
0
using scripts below could achieve the goal.
function defineProperty(obj, field, newValue, writable = true) {
Object.defineProperty(obj, field, {
writable: writable,
value: newValue,
});
}
var localStorageMock = function () {
const obj = {};
return {
getItem: function (key) {
let value = obj[key];
if (value === undefined)
return null;
else
return obj[key] + "";
},
setItem: function (key, value) {
obj[key] = value;
},
removeItem: function (key) {
delete obj[key];
},
clear: function () {
Object.keys(obj).forEach(key => delete obj[key]);
},
getKeys: function () {
return Object.keys(obj);
},
getValues: function () {
return Object.values(obj);
}
}
}
defineProperty(window, 'localStorage', localStorageMock(), false);
//do some test
window.localStorage.setItem("a",110);
console.log("window.localStorage.getItem('a')"+window.localStorage.getItem('a'));

jamie carson
- 1
- 1