简体   繁体   English

OSX WebView中的localStorage在页面重新加载时不持久-Office365插件

[英]localStorage in OSX webview not persisting on page reload - Office365 Addin

I have a problem with localStorage in a webview on OSX (El Capitan + Yosemite). 我在OSX(El Capitan + Yosemite)的Web localStorage中遇到localStorage问题。

I'm making an Office365 app, which on mac uses a webview . 我正在制作一个Office365应用程序,在Mac上使用 Webview。 It all works fine on Windows and even Safari on a Mac - it's just the Mac webview that's proving troublesome. 在Windows上甚至在Mac上的Safari上都可以正常工作-事实证明,只是Mac webview存在麻烦。 It's on a hosted domain, so this isn't the problem. 它位于托管域上,所以不是问题。

As you can see from my test code below, I am saving the info to localStorage, then setting a variable with the value. 从下面的测试代码中可以看到,我将信息保存到localStorage,然后使用该值设置变量。 This means it must have saved to localStorage at some point, but if I refresh the page, the data is lost. 这意味着它一定已经保存到localStorage,但是如果刷新页面,则数据将丢失。 Note: Not all data in my app is lost, just some bits. 注意:并不是我的应用程序中的所有数据都丢失了,只是有些丢失。

I'm not getting a QUOTA_EXCEEDED_ERR on save. 保存时未收到QUOTA_EXCEEDED_ERR

Here is my test code that fails on refresh. 这是我的测试代码,刷新后失败。

$scope.initTest = function () {

    $scope.errorsRecorded += "************** Initialize **************";

    var notNested = localStorage.getItem('lclNotNested');
    if (!notNested) {
        $scope.errorsRecorded += "*_*__NOT NESTED NOT SAVED";
        $scope.notNested = "NOTSET";
    } else {
        $scope.errorsRecorded += "*_*__NOT NESTED ___ val=" + notNested + ';';
        $scope.notNested = JSON.parse(notNested);
    }
};
$scope.initTest();


// Button press event

$scope.setSingle = function () {
    // Set the value to localStorage, then get it back for the UI
    // so we know the value was set.
    try {
        localStorage.setItem('lclNotNested', JSON.stringify(Date.now()));
    } catch (e) {
        $scope.errorsRecorded = $scope.errorsRecorded + '.....<br/>\r\n ' + e;
    }

    $scope.notNested = JSON.parse(localStorage.getItem('lclNotNested'));
};

Here are some screenshots of the values setting correctly and then failing to load. 这是正确设置值然后无法加载的一些屏幕截图。

设置了localStorage

无法加载数据

I've spent hours trying to figure it out and can't find a way to debug it. 我花了数小时试图弄清楚它,却找不到调试它的方法。 I don't have access to the underlying webview code, it's just a web app on top of what Microsoft provides. 我没有访问底层Webview代码的权限,它只是Microsoft提供的功能之上的Web应用程序。

Does anyone know what is going on, or how I might be able to debug it please (eg view a localStorage file for a webview on a mac)? 有谁知道发生了什么事,或者请问如何调试(例如,在Mac上查看Web视图的localStorage文件)?

Debated deleting the question, but this is bound to catch someone else out... 删除问题的辩论,但这势必会吸引其他人...

After a whole load of head scratching and creating this jsfiddle to try in the app, it seems that Office 365 apps don't currently persist localStorage as expected in a webview on OSX. 经过大量的工作量并创建了该jsfiddle并尝试在应用程序中尝试之后,看来Office 365应用程序当前无法像在OSX上的Webview中所预期的那样持久保存localStorage。

My confusion over the partial persistence was that some information was being saved to the document. 我对部分持久性的困惑是某些信息已保存到文档中。 Other (less important) information was being saved to localStorage only and so was not persisting. 其他(不太重要)的信息仅保存到localStorage,因此不会持久保存。

The answer in my case was - if you want persistence over page reload or save and reopen for an Office 365 app - save to the document. 在我的情况下,答案是-如果您希望保留页面重新加载的持久性或保存并重新打开Office 365应用程序-将其保存到文档中。 Don't rely on localStorage for anything. 不要依赖localStorage做任何事情。

Code for jsfiddle: jsfiddle的代码:

<script>


        function deletMany() {

            var getManyNumber = parseInt(document.getElementById('setManyNumber').value);

            for (var i = 0; i < getManyNumber; i++) {
                try {
                    localStorage.removeItem('many' + i);
                } catch (e) {                   
                }
            }

            getMany();
        }

        function getMany() {
            var outputText = '';

            var getManyNumber = parseInt(document.getElementById('setManyNumber').value);


            for (var i = 0; i < getManyNumber; i++) {
                try {
                    var ot = localStorage.getItem('many' + i);

                    outputText += (ot) ? ot + '_' : i + " - no set <br/>";
                } catch (e) {
                    outputText += '|||' + i + '-' + e;
                }
            }

            var elm = document.getElementById('results');
            elm.innerHTML = outputText;
        }

        function setMany() {

            var getManyNumber = parseInt(document.getElementById('setManyNumber').value);

            for (var i = 0; i < getManyNumber; i++) {
                localStorage.setItem('many' + i, "MANY" + i);
            }
        }


        //---------

        function deleteFewWithLots() {

            var getLotsNumber = parseInt(document.getElementById('setLotsNumber').value);

            for (var i = 0; i < getLotsNumber; i++) {
                try {
                    localStorage.removeItem('few' + i);
                } catch (e) {

                }
            }

            getFewWithLots();

        }

        function getFewWithLots() {
            var outputText = '';

            var getLotsNumber = parseInt(document.getElementById('setLotsNumber').value);

            for (var i = 0; i < getLotsNumber; i++) {
                try {
                    var ot = localStorage.getItem('few' + i);

                    outputText += (ot) ? i + ':' + ot + '_' : i + " - no set <br/> \r\n___";
                } catch (e) {
                    outputText += '|||' + i + '-' + e;
                }
            }

            var elm = document.getElementById('results');
            elm.innerHTML = outputText;
        }

        function setFewWithLots() {

            var getLotsNumber = parseInt(document.getElementById('setLotsNumber').value);

            for (var i = 0; i < getLotsNumber; i++) {
                localStorage.setItem('few' + i, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Section 1.10.32 of de Finibus Bonorum et Malorum, written by Cicero in 45 BC. Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur? 1914 translation by H. Rackham. But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?" + i + '______________________________________________________________________ __________________________________________ ____________________________');
            }
        }


    </script>


<body>

    <hr />
    <button onclick="deletMany()">Delete Many</button>  
    <button onclick="getMany()">Get Many</button>
    <input id="setManyNumber" type="number" value="1" style="width:75px;" />
    <button onclick="setMany()">Set Many</button>
    <button onclick="deleteFewWithLots()">Delete Few Items With Lots</button>
    <button onclick="getFewWithLots()">Get Few Items With Lots</button>
    <input id="setLotsNumber" type="number" value="1" style="width:75px;" />
    <button onclick="setFewWithLots()">Set Few Items With Lots</button>
    <hr />
    <div style="background-color:aqua; word-break:break-all; width:500px;margin-left:100px; position: absolute; z-index: 10000;">
        <div id="results"></div>
    </div>

</body>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM