将Javascript代码注入网页

将Javascript代码注入网页,javascript,jquery,html,Javascript,Jquery,Html,我想下载网页作为html文件。在将网页源代码保存到html文件之前,我想先编辑一些网页内容。我假设我可以使用Javascript编辑内容。不幸的是,我对Javascript几乎没有经验。我想我必须将我的脚本注入到网页中,以便浏览器可以一起执行它们。我应该如何写我的脚本?我是否应该编写一个独立的脚本并将页面url传递给脚本,以便它们可以同时执行?或者有其他方法注入我的脚本 编辑:要澄清我的问题,请参见和,因为您只做了一次,从浏览器JavaScript控制台启动脚本就足够了。打开开发人员工具,导航到

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


编辑:要澄清我的问题,请参见和

,因为您只做了一次,从浏览器JavaScript控制台启动脚本就足够了。打开开发人员工具,导航到console选项卡,粘贴脚本内容,然后按enter键


要获取已编辑的HTML,请在控制台中计算表达式
document.documentElement.outerHTML
。将输出复制到您选择的文本编辑器中,在其前面加上doctype,并将其另存为html。

如果您想将修改后的源代码另存为html,则可以使用不同的方法,具体取决于您要维护的内容。遗憾的是,使用javascript保存文件是很棘手的,并且取决于很多事情,所以您可以使用该选项手动复制粘贴文件源,或者编写浏览器和设置特定的文件保存程序。我更喜欢javascript+php组合解决方案。或者,如果不需要使用javascript操作某些东西,我会完全使用php

第1步-在chrome和firefox中打开带有控制台的浏览器CTRL+SHIFT+J并允许弹出窗口。 第2步-打开你想要的网页 步骤3-将下一个代码复制到控制台

//Script loading function
function load_script( source ) {
    var new_script  = document.createElement('script');
    new_script.type = 'text/javascript';
    new_script.src = source;
    new_script.className = 'MyInjectedScript';
    document.getElementsByTagName('head')[0].appendChild(new_script);
}
function escapeHtml(unsafe) {
  return unsafe
      .replace(/&/g, "&")
      .replace(/</g, "&lt;")
      .replace(/>/g, "&gt;")
      .replace(/"/g, "&quot;")
      .replace(/'/g, "&#039;");
}
//Load jQuery, if page do not have it by default
if (typeof(jQuery) != 'function') load_script('http://code.jquery.com/jquery-latest.js');
//In the end remove your injected scripts
$('.MyInjectedScript').remove(); //Or jquery script will be in source
//get Document source
var doc_source = $('html',document).html();
doc_source = '<html>'+doc_source+'</html>';


var new_window = window.open('', '', 'scrollbars=yes,resizable=yes,location=yes,status=yes');
$(new_window.document.body).html('<textarea id="MySource">'+escapeHtml(doc_source)+'</textarea>');
//脚本加载函数
函数加载_脚本(源){
var new_script=document.createElement('script');
new_script.type='text/javascript';
new_script.src=源代码;
new_script.className='MyInjectedScript';
document.getElementsByTagName('head')[0].appendChild(新的_脚本);
}
功能escapeHtml(不安全){
不安全返回
.替换(/&/g,“&;”)
.replace(//g,“”)
.替换(/“/g,”)
.替换(/'/g,';);
}
//如果页面默认没有jQuery,则加载jQuery
if(typeof(jQuery)!='function')加载脚本('http://code.jquery.com/jquery-latest.js');
第4步-在控制台中进行操作

步骤5-将下一个代码复制到控制台

//Script loading function
function load_script( source ) {
    var new_script  = document.createElement('script');
    new_script.type = 'text/javascript';
    new_script.src = source;
    new_script.className = 'MyInjectedScript';
    document.getElementsByTagName('head')[0].appendChild(new_script);
}
function escapeHtml(unsafe) {
  return unsafe
      .replace(/&/g, "&amp;")
      .replace(/</g, "&lt;")
      .replace(/>/g, "&gt;")
      .replace(/"/g, "&quot;")
      .replace(/'/g, "&#039;");
}
//Load jQuery, if page do not have it by default
if (typeof(jQuery) != 'function') load_script('http://code.jquery.com/jquery-latest.js');
//In the end remove your injected scripts
$('.MyInjectedScript').remove(); //Or jquery script will be in source
//get Document source
var doc_source = $('html',document).html();
doc_source = '<html>'+doc_source+'</html>';


var new_window = window.open('', '', 'scrollbars=yes,resizable=yes,location=yes,status=yes');
$(new_window.document.body).html('<textarea id="MySource">'+escapeHtml(doc_source)+'</textarea>');
//最后删除注入的脚本
$('.MyInjectedScript').remove()//或者jquery脚本将位于源代码中
//获取文档源
var doc_source=$('html',document.html();
单据来源=“”+单据来源+“”;
var new_window=window.open('',''滚动条=yes,可调整大小=yes,位置=yes,状态=yes');
$(new_window.document.body).html(“”+escapeHtml(doc_source)+“”);
步骤6-从打开的窗口文本区域复制粘贴代码


如果你想用PHP来实现这一点,你可以很容易地下载带有curl的页面,并根据需要操作内容和保存文件。

在jquery网站上搜索
.getScript()
;你的问题不是很清楚。您想在哪里下载此网页?此任务似乎更适合服务器端脚本,而不是javascript。@浏览器中的DarinDimitrov从浏览器保存HTML文件只保存初始标记,而不保存对DOM的任何javascript更改。@Bergi我不确定。不过,我可以使用Firebug编辑源代码并保存编辑后的代码。为了澄清我的问题,请参阅保护javascript中的“注入”源代码并非易事。你刚才说的“进一步处理”是什么?如果您告诉我们更多有关实际问题的信息,我们可能会建议更好的方法来实现您的目标。我希望使用预定义的标记对数百万网页进行注释,以便它们具有语义,从而可由机器处理。请参阅