简体   繁体   English

Firefox Addon 如何无法从 Popup JS 访问浏览器页面 DOM?

[英]Firefox Addon How to cannot access browser page DOM from the Popup JS?

HiI am trying to develop small simple popup extension in Firefox, I am new to the Firefox extension development Framework.您好我正在尝试在 Firefox 中开发小型简单弹出扩展,我是 Firefox 扩展开发框架的新手。

I used this boilerplate to speed up this process我用这个样板来加速这个过程

I Added a small button to the popup with an id = testid1 by overriding the popup.html file and adding this line我通过覆盖 popup.html 文件并添加此行,在id = testid1的弹出窗口中添加了一个小按钮

<a id="testid1" href="#" class="js-options">Test</a>

I added it the corresponding EventListener by overriding the popup.addEventListener in the popup.js file我通过覆盖 popup.js 文件中的 popup.addEventListener 添加了相应的 EventListener

popup.addEventListener("click", function (e) {
  if (e.target && e.target.matches("#testid1 ")) {

  var element_from_popup_html = document.getElementById("elementid1").value;
  var element_from_web_page = document.getElementById("elementid2").value;
  alert('it work');
}}

I made sure that this event lister triggers correctly and is working by using an Alert.我确保此事件列表器正确触发并使用警报正常工作。 I am trying to get 2 elements from the DOM Element1(inside popup page), and Element2(Fromthe webbrowser)我正在尝试从 DOM Element1(弹出页面内)和 Element2(来自 webbrowser)中获取 2 个元素

I can't get the: element_from_web_page!!我无法得到:element_from_web_page!!

and I have document.getElementById("elementid2") is NULL.我有 document.getElementById("elementid2") 是 NULL。

So After digging some more the: document here is the popup.html, How am I supposed to access this webbrowser browsed page.所以在挖掘了更多之后:这里的文档是popup.html,我应该如何访问这个webbrowser浏览的页面。 I used to use document keyword for this!!我曾经为此使用文档关键字!

Also found out that I can use the Javascript API from mozilla by using _ext2.default.and_put_the_chosen_api_here but I only need simple dom find by id?还发现我可以通过使用 _ext2.default.and_put_the_chosen_api_here 从 mozilla 使用 Javascript API 但我只需要通过 id 查找简单的 dom?

I managed to solve my problem after reading some more about the Firefox Extensions.在阅读了有关 Firefox 扩展的更多信息后,我设法解决了我的问题。 So basically what I was doing is managing the popup document not the webpage.所以基本上我所做的是管理弹出文档而不是网页。 In order to interact with the webbrowser current page I needed to change another type of js called a content script , which is different from the popup.js javascript file(similar to the background script) that I was editing.为了与 webbrowser 当前页面交互,我需要更改另一种类型的 js,称为content script ,它与我正在编辑的 popup.js javascript 文件(类似于背景脚本)不同。

So basically I had to use the communication between the background and content-script with regards to the documentation.所以基本上我不得不在文档方面使用背景和内容脚本之间的通信。

On the content_script.js, I interacted with the web-page using some eval function.在 content_script.js 上,我使用一些 eval function 与网页进行了交互。

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

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