如果您想将修改后的源代码保存为 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, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
//Load jQuery, if page do not have it by default
if (typeof(jQuery) != 'function') load_script('http://code.jquery.com/jquery-latest.js');
第 4 步 - 在控制台中进行操作
第 5 步 - 将下一个代码复制到控制台
//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>');
第 6 步 - 从打开的窗口文本区域复制粘贴代码
如果你想用 PHP 来做,你可以很容易地用 curl 下载页面并根据需要操作内容和保存文件。