简体   繁体   English

如何使用 javascript 获取当前焦点(活动)网页的 DOM 树

[英]How to get currently focused(active) webpage's DOM tree using javascript

I am trying to make a java software in which I am giving an option to switch to 'Browser' mode.我正在尝试制作 java 软件,在该软件中我可以选择切换到“浏览器”模式。 As soon as the user selects it and visits any web page, the user can select any word from the current webpage and see its definition.一旦用户选择它并访问任何 web 页面,用户就可以从当前网页 select 任何单词并查看其定义。 The user should be able to switch between web pages and still leverage the functionality(and hence the current active web page's DOM tree).用户应该能够在 web 页面之间切换并仍然利用该功能(因此当前活动的 web 页面的 DOM 树)。 Thought of creating a Mozilla or Chrome extension first, but this has to be the part of Java software and browsers these days don't allow installing extension via a software.考虑先创建一个 Mozilla 或 Chrome 扩展程序,但这必须是 Java 软件的一部分,而如今的浏览器不允许通过软件安装扩展程序。 I have tried: window.document.getElementsByTagName('p')[0].innerHTML to get all the <p> content of the current page but it doesn't work.我试过: window.document.getElementsByTagName('p')[0].innerHTML来获取当前页面的所有<p>内容,但它不起作用。

To return all the <p> contents you can do:要返回所有<p>内容,您可以执行以下操作:

var items = [...window.document.getElementsByTagName('p')];
var result = items.map(obj => obj.innerHTML);

It will return an array of contents它将返回一个内容数组

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

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