admin管理员组文章数量:1389874
I'm new to the world of Chrome extensions / auto-downloading. I have a background page which takes a screenshot of the visible webpage with chrome.tabs.captureVisibleTab()
. In my popup I have:
chrome.tabs.captureVisibleTab(null, {}, function (image) {
// Here I want to automatically download the image
});
I've done something similar with a blob
before but I'm totally at a loss as to how to download an image as well as how to do it automatically.
In practice, I want my Chrome extension to screenshot + download the image automatically whenever a particular page is loaded (I'm guessing this will have to be achieved by having my content script talk to my background page, correct?)
I'm new to the world of Chrome extensions / auto-downloading. I have a background page which takes a screenshot of the visible webpage with chrome.tabs.captureVisibleTab()
. In my popup I have:
chrome.tabs.captureVisibleTab(null, {}, function (image) {
// Here I want to automatically download the image
});
I've done something similar with a blob
before but I'm totally at a loss as to how to download an image as well as how to do it automatically.
In practice, I want my Chrome extension to screenshot + download the image automatically whenever a particular page is loaded (I'm guessing this will have to be achieved by having my content script talk to my background page, correct?)
Share Improve this question asked Mar 26, 2015 at 9:53 JVGJVG 21.2k48 gold badges141 silver badges216 bronze badges2 Answers
Reset to default 7Yes, as you said you can use Message Passing to get it done. By content scripts to detect the switch on particular pages, then chat with the background page in order to capture screenshot for that page. Your content script should send a message using chrome.runtime.sendMessage
, and the background page should listen using chrome.runtime.onMessage.addListener
:
Sample codes I created and tested it works with me:
Content script(myscript.js):
chrome.runtime.sendMessage({greeting: "hello"}, function(response) {
});
Background.js:
var screenshot = {
content : document.createElement("canvas"),
data : '',
init : function() {
this.initEvents();
},
saveScreenshot : function() {
var image = new Image();
image.onload = function() {
var canvas = screenshot.content;
canvas.width = image.width;
canvas.height = image.height;
var context = canvas.getContext("2d");
context.drawImage(image, 0, 0);
// save the image
var link = document.createElement('a');
link.download = "download.png";
link.href = screenshot.content.toDataURL();
link.click();
screenshot.data = '';
};
image.src = screenshot.data;
},
initEvents : function() {
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.greeting == "hello") {
chrome.tabs.captureVisibleTab(null, {format : "png"}, function(data) {
screenshot.data = data;
screenshot.saveScreenshot();
});
}
});
}
};
screenshot.init();
Also keep in mind to register your content script's code and permissions in manifest file:
"permissions": ["<all_urls>","tabs"],
"content_scripts": [
{
"matches": ["http://www.particularpageone./*", "http://www.particularpagetwo./*"],
"js": ["myscript.js"]
}
]
It captures the screenshot and download the image automatically as .png whenever a particular page is loaded. Cheers!
Here's an alternative to message passing that should acplish the same:
In my manifest.json
"manifest_version": 3,
"permissions": ["activeTab", "scripting"],
"background": {
"service_worker": "background.js"
},
...
In background.js
const download = (dataurl, filename) => {
const link = document.createElement("a");
link.href = dataurl;
link.download = filename;
link.click();
}
chrome.action.onClicked.addListener((tab) => {
chrome.tabs.captureVisibleTab(async (dataUrl) => {
await chrome.scripting.executeScript({
func: download,
target: { tabId: tab.id },
args: [dataUrl, 'test.png'],
});
});
});
本文标签:
版权声明:本文标题:javascript - Chrome Extension: Automatically download a screenshot taken with 'chrome.tabs.captureVisibleTab' - 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744690626a2619974.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论