使用 Chrome 扩展程序保存和读取当前页面
我正在开发一个 chrome 扩展,需要保存当前加载的页面(.js 和 .html),然后生成该加载页面的 SHA1 哈希值。我到处都读到 Chrome 扩展不允许加载不属于其扩展目录并且需要使用 NPAPI 的文件。
因此,我需要一些建议来了解实现这一目标的最佳方法是什么?
非常感谢任何帮助!
I'm developing a chrome extension that needs to save the current loaded page(.js and .html) and then generate a SHA1 hash of that loaded page. I have read everywhere that chrome extension doesn't allow loading a file which doesn't belong to its extension directory and is required to use NPAPI.
So I need some advice on what could be the best way for me to accomplish this?
Any help is greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实现此目的的一种方法是让后台页面调用返回所需项目的内容脚本,将以下内容返回到后台页面:
document.documentElement.innerHTML 用于 HTML
document.scripts[].innerHTML 用于脚本
如果您有外部 javascript,则在后台页面中通过 XMLHttpRequest 重新请求 js 文件。否则,如果脚本是内联的,那么您可以简单地对其进行哈希处理。
获得所有文件后,您可以轻松地执行哈希,然后保存文件。正如本文建议的 POST/GET 或更好的 HTML5 本地存储: Chrome 扩展:如何在磁盘上保存文件
这是一篇很好的文章,展示了如何进行通信:
http://markashleybell.com/building-a-simple-google- chrome-extension.html
One way you could achieve this would be to have your background page call your content script that returns the items you need, returning the following to your background page:
document.documentElement.innerHTML for the HTML
document.scripts[].innerHTML for the scripts
If you have external javascript then re-request the js file by XMLHttpRequest in your background page. Otherwise if the script is inline then you can simply just hash that.
Once you have all the files you can easily perform your hash and then save the file. As this article suggests either the POST/GET or better HTML5 local storage: Chrome extension: How to save a file on disk
This is a good article that shows how to get the communication going:
http://markashleybell.com/building-a-simple-google-chrome-extension.html