首页 > 解决方案 > 如何从嵌入式页面访问父变量?

问题描述

我想创建一个出现在网页上的便笺,当我键入内容时,内容与 URL 一起存储在数据库中作为参考

例如,我去一个 BBC 页面,我点击 cmd+escap 并出现我的便笺(这是一个Tampermonkey脚本)

function addEmbed(){ 
    localStorage.setItem("myUniqueAPIcode", window.location);
    var iframe = document.createElement("iframe");
  iframe.type="text/html"
  iframe.src="http://localhost:8080/webhighlights"
  iframe.style.cssText = `position: absolute;
  top: 0;
  right: 0;
  z-index: 999999;
  height: 100vh;` 
  return iframe
}

该代码有效,因为便笺出现了。这是一个 Vue 页面。但我无法访问本地存储值

mounted() {
    if (typeof Storage !== "undefined") {
      if (localStorage.myUniqueAPIcode) {
        this.api = localStorage.myUniqueAPIcode
      }
    }
       
  },
};

我有点知道为什么,tampermonkey 脚本在 BBC 页面上设置值,而 Vue 代码在 localhost 页面中查找。

但是,在此之前,我尝试将粘性表单附加到页面本身,但某些网站不允许执行获取请求。

有没有办法做到这一点?

标签: javascript

解决方案


推荐阅读