强制阅读器在浏览器中的视图 /修改JS中所有A标签的内容
第一次在这里发布。让我说我还不那么精通JS来序言。 我正在尝试编写一个简单的JavaScript脚本,该脚本强迫阅读器视图以清除链接的网页(DOM操作)。我在网上寻找一种很好的方法,一开始我认为Node.Interbefore()是一个好方法,但是我意识到这会创建一个新的节点,而不是修改现有节点,所以我来了要堆叠以尝试找到一种很好的方法,但是我找不到令人满意的答案。
我想解决的方式正在修改文档中的所有元素要具有“关于:读者?url ='在URL之前,然后在此操作后,查看用户正在使用的浏览器的检查,并修改代码以确保代码与用户浏览器兼容。只有在这里,我发现Chrome用户需要手动启用实验标志,这可能会造成一些麻烦。
我的代码看起来像这样:
html:
<body>
<div id="parentElement">
<a href="stackoverflow.com/questions">Click here</a>
</div>
<script src="js\script.js"></script>
</body>
JS:
var readerText = document.createTextNode('about:reader?url=');
var link = document.getElementsByTagName('a');
link.parentNode.insertBefore(readerText, link);
// Get the parent element
let parentElement = document.getElementById('parentElement')
// Get the parent's first child
let theFirstChild = parentElement.firstChild
// Create a new element
let newElement = document.createElement("div")
// Insert the new element before the first child
parentElement.insertBefore(newElement, theFirstChild)
任何帮助都将不仅值得赞赏,不仅是代码建议,而且对不同的浏览器如何接近读取器的视图以及是否有可行的方法来始终如一地强制读取器在不同的浏览器中的视图。 谢谢你! - serapham
first time posting on here. Let me preface this by saying that I am not that well versed in JS yet.
I am trying to write a simple JavaScript script that forces reader view for clear viewing of a linked webpage (DOM manipulation). I was looking online for a good way to do this, and at first I thought Node.insertBefore() would be a good way, but I came to realize that this would create a new node instead of modifying the existing one, so I came to Stackoverflow to try and find a good way of doing this but I could not find a satisfying answer.
The way I want to go about it is modifying all a elements in the document to have 'about:reader?url=' before the URL, and after that is working, a check to see what browser the user is using, and modify the code to ensure the code is compatible with the users browser. Only here I found out that Chrome users require the manual enabling of an experimental flag, which will likely cause some trouble.
My code looks like this:
HTML:
<body>
<div id="parentElement">
<a href="stackoverflow.com/questions">Click here</a>
</div>
<script src="js\script.js"></script>
</body>
JS:
var readerText = document.createTextNode('about:reader?url=');
var link = document.getElementsByTagName('a');
link.parentNode.insertBefore(readerText, link);
// Get the parent element
let parentElement = document.getElementById('parentElement')
// Get the parent's first child
let theFirstChild = parentElement.firstChild
// Create a new element
let newElement = document.createElement("div")
// Insert the new element before the first child
parentElement.insertBefore(newElement, theFirstChild)
Any help would be greatly appreciated, not only are code suggestions welcome, but also any knowledge about how different browsers approach reader view and if there is a feasible way to consistently force reader view in different browsers.
Thank you!
-Seraphym
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论