abh*_*ash 8 javascript jquery google-chrome google-chrome-extension
chrome.tabs.query({
active: true,
currentWindow: true
},
function (tabs) {
chrome.tabs.captureVisibleTab(null
,{ format: "png"},
function (src) {
$('body').append("<img src='" + src + "'>" + tabs[0].url + "</img>");//appends captured image to the popup.html
}
);
});
Run Code Online (Sandbox Code Playgroud)
此代码将捕获的图像附加到popup.html的主体中.但我想要的是将图像附加到弹出框体,我想使用chrome.tabs.create({url:"newtab.html")打开新标签,并将捕获的图像附加到此newtab.html.( 'newtab.html'已经在路径文件夹中).
提前致谢
我之前在这里描述过一种方法。
要点是打开一个包含脚本的选项卡,并使用消息传递与其进行通信。
出现的一个小问题是你不知道新打开的页面何时准备好。我通过让新打开的页面自行联系后台页面来解决这个问题,但由于只有一个全局变量,所以很草率。
更好的解决方案是一次性事件侦听器,如下所示:
// Execute this code from the background page, not the popup!
function openScreenshot(src){
chrome.tabs.create(
{ url: chrome.runtime.getURL("newtab.html") },
function(tab) {
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
// If the request is expected and comes from the tab we just opened
if(message.getScreenshot && sender.tab.id == tab.id) {
sendResponse(src);
// Ensure we're only run once
chrome.runtime.onMessage.removeListener(arguments.callee);
}
});
}
);
}
Run Code Online (Sandbox Code Playgroud)
除此之外,请按照链接的答案进行操作。
| 归档时间: |
|
| 查看次数: |
896 次 |
| 最近记录: |