同源下禁用iframe右键菜单

5 阅读1分钟
<!DOCTYPE html><html lang="en">  <head>    <meta charset="UTF-8" />    <meta name="viewport" content="width=device-width, initial-scale=1.0" />    <title>Document</title>    <style>      /* 禁用文本选择,增强右键菜单禁用效果 */      body {        -webkit-user-select: none;        -moz-user-select: none;        -ms-user-select: none;        user-select: none;        margin: 0;        padding: 0;        overflow: hidden;      }    </style>  </head>  <body>    <iframe      id="iframe"      src="docx-preview.html?file=11111.docx"      style="width: 100%; height: 100vh; border: none"    ></iframe>    <script>      const iframe = document.getElementById("iframe");      // 禁用 iframe 内部内容的右键菜单(同源时有效)      function disableIframeContextMenu() {        try {          // 方法1: 通过 contentDocument 访问          if (iframe.contentDocument) {            const doc = iframe.contentDocument;            // 禁用右键菜单            doc.addEventListener(              "contextmenu",              function (e) {                e.preventDefault();                e.stopPropagation();                return false;              },              true            );            // 禁用右键按下            doc.addEventListener(              "mousedown",              function (e) {                if (e.button === 2) {                  e.preventDefault();                  e.stopPropagation();                  return false;                }              },              true            );            // 禁用右键释放            doc.addEventListener(              "mouseup",              function (e) {                if (e.button === 2) {                  e.preventDefault();                  e.stopPropagation();                  return false;                }              },              true            );            // 在 body 上直接设置            if (doc.body) {              doc.body.oncontextmenu = function () {                return false;              };            }            console.log("✅ 成功禁用 iframe 内部右键菜单");          }          // 方法2: 通过 contentWindow 访问          else if (iframe.contentWindow && iframe.contentWindow.document) {            const doc = iframe.contentWindow.document;            doc.addEventListener(              "contextmenu",              function (e) {                e.preventDefault();                e.stopPropagation();                return false;              },              true            );            if (doc.body) {              doc.body.oncontextmenu = function () {                return false;              };            }            console.log(              "✅ 成功禁用 iframe 内部右键菜单(通过 contentWindow)"            );          } else {            console.log("⚠️ iframe 内容尚未加载完成");          }        } catch (e) {          console.error("❌ 无法访问 iframe 内容:", e.message);        }      }      // 等待 iframe 加载完成      iframe.onload = function () {        // 延迟一下,确保内容完全加载        setTimeout(() => {          disableIframeContextMenu();        }, 100);      };      // 如果 iframe 已经加载完成,立即执行      if (        iframe.contentDocument ||        (iframe.contentWindow && iframe.contentWindow.document)      ) {        setTimeout(() => {          disableIframeContextMenu();        }, 100);      }    </script>  </body></html>