在新的Google站点中使用localStorage和IndexedDB〈嵌入式HTML>不起作用

qcuzuvrc  于 2023-02-14  发布在  IndexedDB
关注(0)|答案(1)|浏览(262)

我正在尝试使用新的谷歌网站的网页,我已经开发,但是,我有麻烦存储本地数据。本地文件在windows和苹果safari/chrome工作正常。尝试从谷歌网站,并没有喜悦!此外,在safari,抛出一个错误,"www.example.com()调用在一个无效的安全上下文"。IDBFactory.open() called in an invalid security context".
我真的很想通过谷歌网站托管我的网站,而不需要链接到其他服务器。我特别需要一些小项目的本地持久数据。我似乎也不能让cookie工作。
有什么建议吗?
我在Windows 10 Surface Pro 2017、运行Safari 12.2的苹果iPad、运行macOs Mojave 10.14的苹果Mac Mini上都试过了。我从Windows 10命令行使用SimpleHTTPServer作为网络服务器共享文件。我还通过电子邮件发送文件并直接在指定系统上打开。最后,我在https://sites.google.com/view/gerrymap上创建了一个新的谷歌网站。只是一个嵌入式HTML元素与下面的文本复制到源代码编辑框。2欢迎所有点击该页,如果他们愿意。3否则,使用下面的简短张贴文件。
说明在HTML页面本身中。
所有代码在html文件的本地示例中运行良好。可以为lat、long、rad和key输入新值,保存并读取它们。我也可以刷新页面,然后读取它们而不先存储,没有任何问题。这证明了这些值不仅仅是会话持久的。
从Google站点则是另一回事。我设置了一个使用html文件的站点。当我输入新值并按保存按钮时,IndexedDB失败,但localStorage成功返回保存的值。但是,如果我按刷新按钮,然后读取值而不先尝试存储,IndexedDB再次失败,但localStorage也失败,因为它不检索任何值。
我相信我已经正确地实现了代码(尽管有些人可能会反对,我敢肯定。这里没有骄傲,欢迎批评)。
我做了很多google搜索,特别是关于google站点和indexeddb/localstorage的,我也在google社区帮助论坛上发帖,但仍然没有成功。
目前,我没有后备方法,但需要一些相对简单的东西。有人能给我一点快乐吗?提前感谢!

<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <title>Test Local Storage</title>
    <style>
    </style>
</head>

<body onload="initializeValues()">
    Instructions:  <br />
    1.  Save this sample code in an html file locally and open in a browser.<br />
    2.  Enter different values into the lat, long, rad, and key edit boxes.<br />
    3.  Press TrySave to store the values in indexedDB and localStorage.<br />
    4.  Refresh the webpage from the browser address line<br />
    5.  Press the individual Try IndexedDB and Try LocalStorage buttons to attempt<br />
    6.  Try inserting this code into a New Google Site, or access https://sites.google.com/view/gerrymap/home <br />
    <br>
    <input id="latitude" /> Latitude<br><br>
    <input id="longitude" /> Longitude<br><br>
    <input id="radius" /> Radius<br><br>
    <input id="key" /> Key<br><br>

    <button onclick="TryIndexedDB()" title="This tries to load via IndexedDB">Try IndexedDB</button><br><br>
    <button onclick="TryLocalStorage()" title="This tries to load via localStorage">Try localStorage</button><br><br>
    <button onclick="trySave()" title="This tries to save the data in both methods (IndexedDB, localStorage)">Try Save</button><br><br>
    <button onclick="clearAll()" title="Clear the log space at the bottom of this example page">Clear Log</button><br><br>

    <div id="hello">
    </div>

    <script>
        "use strict";

        function clearAll() {
            document.getElementById("hello").innerHTML = "";
        }

        // tagBeginDefaultsReplace
        var navLatitude = 39;
        var navLongitude = -76.7;
        var navMaxDist = 200;
        var navKey = "PleaseAddYourKey";

        function initializeValues() {
            document.getElementById("latitude").value = navLatitude;
            document.getElementById("longitude").value = navLongitude;
            document.getElementById("radius").value = navMaxDist;
            document.getElementById("key").value = navKey;
        }

        function trySave() {
            navLatitude = document.getElementById("latitude").value;
            navLongitude = document.getElementById("longitude").value;
            navMaxDist = document.getElementById("radius").value;
            navKey = document.getElementById("key").value;

            // Save using indexeddb
            getLocationDB(true, FinishIndexedDB);
            // Save using localStorage
            localStorage.setItem('latitude', navLatitude.toString());
            localStorage.setItem('longitude', navLongitude.toString());
            localStorage.setItem('radius', navMaxDist.toString());
            localStorage.setItem('key', navKey.toString());
            mylog("Done saving localStorage");
        }

        function getLocationDB(bSave, callbackf) {
            var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB;
            var openDB;

            try {
                var myitem;

                openDB = indexedDB.open("SampleDatabase", 1);
                openDB.onupgradeneeded = function () {
                    var db = openDB.result;
                    var store = db.createObjectStore("SampleStore", { keyPath: "id" });
                    var index = store.createIndex("PosIndex", ["pos.latitude", "pos.longitude", "pos.radius", "pos.navkey"]);
                };

                openDB.onsuccess = function () {
                    // Start a new transaction var db = openDB.result;
                    callbackf("Successfully opened openDB");
                    var db = openDB.result;
                    var tx = db.transaction("SampleStore", "readwrite");
                    var store = tx.objectStore("SampleStore");
                    if (bSave) {
                        if (navLatitude != undefined && navLongitude != undefined && navMaxDist != undefined)
                            store.put({ id: 0, pos: { latitude: navLatitude, longitude: navLongitude, radius: navMaxDist, navkey: navKey } });
                        else
                            store.put({ id: 0, pos: { latitude: "38", longitude: "-75.7", radius: "100", navkey: "Please Enter Mapbox Key" } });
                        callbackf("Save indexeddb finished");
                    }
                    else {
                        var getNavLoc = store.get(0);

                        getNavLoc.onsuccess = function () {
                            if (getNavLoc != undefined
                                && getNavLoc.result != undefined) {
                                callbackf("Succeeded reading from store.  Result=" + JSON.stringify(getNavLoc.result));
                                navLatitude = parseFloat(getNavLoc.result.pos.latitude);
                                navLongitude = parseFloat(getNavLoc.result.pos.longitude);
                                navMaxDist = parseFloat(getNavLoc.result.pos.radius);
                                navKey = getNavLoc.result.pos.navkey;
                            }
                            else {
                                callbackf("Succeeded reading from store.  Result=undefined");
                                navLatitude = navLongitude = navMaxDist = navKey = "undef";
                            }
                            initializeValues();
                        }

                        getNavLoc.onerror = function () {
                            callbackf("An error occurred getting indexeddb");
                        }

                    }
                }

                openDB.onerror = function () {
                    callbackf("An error occurred opening openDB");
                }
            }
            catch (e) {
                callbackf("Caught error in try block of indexeddb:  " + e.Message);
            }

        }

        function TryIndexedDB() {
            getLocationDB(false, FinishIndexedDB);
        }

        function TryLocalStorage() {
            mylog("localStorage read");
            navLatitude = localStorage.getItem('latitude');
            mylog("latitude=" + navLatitude);
            navLongitude = localStorage.getItem('longitude');
            mylog("longitude=" + navLongitude);
            navMaxDist = localStorage.getItem('radius');
            mylog("radius=" + navMaxDist);
            navKey = localStorage.getItem('key');
            mylog("key=" + navKey);
            if (navLatitude == undefined)
                navLatitude = "undef";
            if (navLongitude == undefined)
                navLongitude = "undef";
            if (navMaxDist == undefined)
                navMaxDist = "undef";
            if (navKey == undefined)
                navKey = "undef";
            initializeValues();
        }

        function FinishIndexedDB(nSucceeded) {
            mylog(nSucceeded);
        }

        function mylog(logstr) {
            document.getElementById("hello").innerHTML += "<br>" + logstr.toString();
        }

    </script>
</body>
</html >
mwg9r5ms

mwg9r5ms1#

问题是Google网站提供iframe内容的方式。我不确定幕后的确切细节,但似乎每次页面加载时都有一个随机生成的域。由于localStorageIndexedDB与特定的域相关联,这会导致页面重新加载时保存的数据“丢失”。
作为一个例子,下面是我第一次加载页面时iframe的数据:

下面是刷新页面后iframe的数据:

正如您所看到的,刷新后域完全不同,这意味着它有一个全新的空数据库。

相关问题