从 Office 加载项中记录 iframe(url) 的状态

德尔博伊尔

所以我正在开发一个办公室插件,其中基本上包含一个 iframe,它将运行我们拥有的应用程序。

问题是,我想不断地记录 iframe 的 url,以便我可以将其保存到插件状态,每次重新打开插件时,我们都可以使用该信息将 iframe 加载到正确的 url。

每次 iframe 更改时,我都想不出一种从 iframe 中输出 url 的方法?这是我所拥有的示例,它不包含应用程序,仅包含几个示例页面:

主页.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <title></title>
    <script src="../Scripts/jquery-1.9.1.js" type="text/javascript"></script>
    <script src="../Scripts/FabricUI/MessageBanner.js" type="text/javascript"></script>
    <script src="https://appsforoffice.microsoft.com/lib/1/hosted/office.js" type="text/javascript"></script>

    <link href="Home.css" rel="stylesheet" type="text/css" />
    <script src="Home.js" type="text/javascript"></script>

    <!-- For the Office UI Fabric, go to https://aka.ms/office-ui-fabric to learn more. -->
    <link rel="stylesheet" href="https://appsforoffice.microsoft.com/fabric/2.1.0/fabric.min.css">
    <link rel="stylesheet" href="https://appsforoffice.microsoft.com/fabric/2.1.0/fabric.components.min.css">


</head>
<body>


</body>
</html>

主页.js

(function () {
    "use strict";

    // The initialize function must be run each time a new page is loaded
    Office.initialize = function (reason) {
        $(document).ready(function () {

            var iframew = document.createElement('iframe');
            iframew.src = '../SecondPage/SecondPage.html';
            iframew.id = 'iframe1';
            iframew.onload = iframeLoaded(this.contentWindow.location.href);

            document.body.appendChild(iframew);


        });
    };

    // Helper function for displaying notifications

    function iframeLoaded(location) {
        console.log("log", location);
    }
})();
德尔博伊尔

无法理解为什么人们不赞成这个问题,这是一个真正的问题

无论如何,对于任何有兴趣的人,我通过在 iframe 加载后附加一个函数来解决这个问题,该函数以设定的间隔时间输出 iframe 的 url

var iframew = document.createElement('iframe');
        iframew.id = 'iframe1';
        var baseUrl = '#YOUR BASE URL#';

        let openUrl = getProperty('openurl');
        if (!openUrl) {
            console.log('No saved url');
            iframew.src = baseUrl;

        }
        else {
            console.log('saved url');
            console.log(openUrl);
            iframew.src = openUrl;

        }
        //when iframe loads attach function to save at interval
        iframew.addEventListener('load', function () { setInterval(function () { iframeLoaded(iframew.contentWindow.location.hash, iframew.contentWindow.location.href); }, 4000); });
        document.body.appendChild(iframew);

这是 iframeLoaded 函数,它也对 url 进行一些操作并调用另一个函数,该函数将 url 保存到加载项的文档设置中:

function iframeLoaded(hash, location) {
            //if not in an analysis dont save
            if (hash.indexOf('#/dataset/') !== -1 ) {
                console.log("Same url")
                return
            }
           //remove # from hash
            hash = hash.substr(1);
            //concatenate base and hash
            let newUrl = baseUrl + hash;
            console.log(hash)
            console.log(newUrl);
            //save
            if (Office.context.document.settings) {
                saveProperty('openurl', newUrl);
            }
        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从VSTO Outlook加载项中检测Office 365加载项是否已处于活动状态,反之亦然?

如何在COM加载项中获取Office文档的IRM加密状态?

在文档加载时使Office加载项任务窗格保持打开状态

Access和Office加载项复选框处于选中状态

Office JS加载项

打包Office加载项

在Powerpoint Online中测试Office加载项

在Office加载项中查找当前主机

StorageEvent 在 Office 加载项中如何工作?

Office商店中Office加载项的付款选项

无法加载 Office 加载项

Office Word 加载项 JS - 如何避免加载项执行的操作出现在撤消历史记录中

在Office加载项中启用扩展错误日志记录(以查看有关错误对象的完整语句)

Office 2013的Excel加载项

Mac中未加载office加载项-Mac中是否支持Office.initialize事件?office.js

将Office VSTO加载项发布到办公商店中

Office加载项在清单中明确提到平台支持

将Word Online检测为Office加载项中的主机

我的 Office 加载项中的 Javascript 位于哪个域

仍在安装时从功能区中消失的 Office 加载项

Office 365组日历中的自定义加载项

加载项无法在Office Professional Plus 2016中运行

如何在Office JavaScript加载项中调用.NET dll?

Word 加载项未出现在 Office 商店中

从 office 加载项,是否可以获取 office 文档的加载位置?

新的Microsoft Word Office加载项在功能区中未显示加载项按钮

Microsoft对发布到Office加载项存储中的加载项托管什么?

使用Office JS API的Office VSTO加载项与Office加载项

如何通过Xamarin.forms中的Office365 API存储身份验证状态