Sharepoint Online 无法隐藏 iFrame 功能区

Sharepoint Online Can't Hide iFrame ribbon

我试图在 sharepint 中隐藏 iframe 0365 功能区,但它只是返回这个...

TypeError: document.getElementById(...) is null

这是我正在测试的代码,我最终会使用 display:none...

document.getElementById('.content-frame').contentDocument.body.getElementById('#suiteBarDelta').style.visibility = 'hidden';

有更好的方法吗?

假设您要将此脚本直接添加到包含 "parent" window 代码(即它具有 iframe)的 html 文件中。确保在执行此代码时定义了 iframe(因此,将脚本块放在 iframe 元素之后,或使用 window.onload 或 document.ready 处理程序)

<iframe class='content-frame'></iframe>

...

<script>
document.getElementsByClassName("content-frame")[0].addEventListener("load", function () {
  // the document in the iframe has loaded.  let's hide that ribbon bar
  var ribbonBar = this.contentDocument.getElementById("suiteBarDelta");
  if (ribbonBar) {
    ribbonBar.style.visibility = 'hidden';
  } else {
    console.log("uh oh, ribbonBar still does not exist");
  }    
});
</script>

我真的不知道 Sharepoint 是否做了很多恶作剧来构建 iframe 中的内容(例如,调用异步请求来获取功能区栏)——但这肯定会破坏此代码(你会得到控制台中的呃哦消息)。