将Javascript代码注入网页 [英] Inject Javascript code into a web page

查看:94
本文介绍了将Javascript代码注入网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将网页下载为html文件。在将网页源代码保存为html文件之前,我想先编辑一些页面内容。我假设我可以使用Javascript编辑内容。不幸的是,我对Javascript没有多少经验。我想我必须将我的脚本注入网页,以便浏览器可以一起执行它们。我应该如何编写我的脚本?我应该编写一个独立的脚本并将页面url传递给我的脚本,以便它们可以同时执行吗?或者还有其他的方法来注入我的脚本?

编辑:为了让我的问题清楚,请参阅这篇文章这篇文章

解决方案

由于您只做过一次,所以从浏览器JavaScript控制台启动脚本应该足够了。打开开发人员工具,导航到控制台选项卡,粘贴脚本内容,然后按Enter键。



要获取编辑的HTML,请评估表达式 document.documentElement.outerHTML 在控制台中。将输出复制到您选择的文本编辑器中,将其前缀为文档类型,并将其另存为html。


I'd like to download web page as html file. Before I save the web page source code in html file, I'd like to edit some of the page content first. I assume I can edit the content using Javascript. Unfortunately I have little experience with Javascript. I guess I have to inject my script into the web page so that the browser can execute them together. How should I write my script? Should I write a standalone script and pass the page url to my script so that they can be executed at the same time? Or there are other ways to inject my script?

EDIT: To make my problem clear, see this post and this post

解决方案

As you are only doing this once, starting your script from the browsers JavaScript console should be enough. Open the developer tools, navigate to the console tab, paste your script content, and press enter.

To get the edited HTML, evaluate the expression document.documentElement.outerHTML in the console. Copy the output to a text editor of your choice, prepend it with a doctype, and save it as html.

这篇关于将Javascript代码注入网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆