我们可以使用chrome.downloads.download完全下载网页吗? (Google Chrome扩展程序) [英] Can we download a webpage completely with chrome.downloads.download? (Google Chrome Extension)
问题描述
我想从我的Google Chrome扩展程序中完全保存wabpage。
我添加了downloads,< all_urls>
权限,并确认以下代码将Google页面保存为 google.html
。
chrome.downloads.download(
{url:http:// www.google.com,
文件名:google.html},
函数(x){console.log(x);})
但是,此代码仅保存html文件。
样式表,脚本和图片不会被保存。
我想完全保存网页,就好像我用对话框保存页面,选择格式:网页,完成
。
我查看了文档,但我无法找到一个方法。
所以我的问题是:如何使用Google Chrome的api完全从扩展中下载网页?
downloads
API仅下载单个资源。如果要保存完整的网页,则可以先打开网页,然后使用 chrome.pageCapture.saveAsMHTML
,为导出的创建一个
使用 blob:
-URL Blob URL.createObjectURL
,最后使用 chrome.downloads.download
API。
pageCapture API需要有效的标签。例如:
//创建新标签,等到它被加载并保存页面
chrome.tabs。 create({
url:'http://example.com'
},function(tab){
chrome.tabs.onUpdated.addListener(function func(tabId,changeInfo){
if(tabId == tab.id&&; changeInfo.status =='complete'){
chrome.tabs.onUpdated.removeListener(func);
savePage(tabId);
}
});
});
function b
$ b $ URL.createObjectURL(blob);
//可选:chrome.tabs.remove(tabId); //关闭标签
chrome.downloads.download({
url:url,
文件名:'whatever.mhtml'
});
});
}
试一试,将前面的代码放在后台.js
,
将权限添加到 manifest.json
(如下所示)并重新加载扩展。然后打开example.com,网页将被保存为自包含的 MHTML 文件。
{
name:保存完整的网页,
version:1,
manifest_version:2,
background:{
scripts:[background.js]
},
permissions:[
pageCapture,
下载
]
}
I want to save a wabpage completely from my Google Chrome extension.
I added "downloads", "<all_urls>"
permissions and confirmed that the following code save the Google page to google.html
.
chrome.downloads.download(
{ url: "http://www.google.com",
filename: "google.html" },
function (x) { console.log(x); })
However, this code only saves the html file.
Stylesheets, scripts and images are not be saved.
I want to save the webpage completely, as if I save the page with the dialog, selecting Format: Webpage, Complete
.
I looked into the document but I couldn't find a way.
So my question is: how can I download a webpage completely from an extension using the api(s) of Google Chrome?
The downloads
API downloads a single resource only. If you want to save a complete web page, then you can first open the web page, then export it as MHTML using chrome.pageCapture.saveAsMHTML
, create a blob:
-URL for the exported Blob
using URL.createObjectURL
and finally save this URL using the chrome.downloads.download
API.
The pageCapture API requires a valid tabId. For instance:
// Create new tab, wait until it is loaded and save the page
chrome.tabs.create({
url: 'http://example.com'
}, function(tab) {
chrome.tabs.onUpdated.addListener(function func(tabId, changeInfo) {
if (tabId == tab.id && changeInfo.status == 'complete') {
chrome.tabs.onUpdated.removeListener(func);
savePage(tabId);
}
});
});
function savePage(tabId) {
chrome.pageCapture.saveAsMHTML({
tabId: tabId
}, function(blob) {
var url = URL.createObjectURL(blob);
// Optional: chrome.tabs.remove(tabId); // to close the tab
chrome.downloads.download({
url: url,
filename: 'whatever.mhtml'
});
});
}
To try out, put the previous code in background.js
,
add the permissions to manifest.json
(as shown below) and reload the extension. Then example.com will be opened, and the web page will be saved as a self-contained MHTML file.
{
"name": "Save full web page",
"version": "1",
"manifest_version": 2,
"background": {
"scripts": ["background.js"]
},
"permissions": [
"pageCapture",
"downloads"
]
}
这篇关于我们可以使用chrome.downloads.download完全下载网页吗? (Google Chrome扩展程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!