gpt4 book ai didi

javascript - tabs.saveAsPDF() 不工作(Firefox Web-ext)

转载 作者:行者123 更新时间:2023-11-30 21:06:11 25 4
gpt4 key购买 nike

所以,我昨天得到了 Firefox 56 (Ubuntu Gnome),我开始尝试 tabs.saveAsPDF()功能(Firefox 56+)。所以在site他们展示的示例是针对背景脚本的。但我只想在按下按钮时触发它。所以我制作了一个按钮,并在 .js 文件(弹出窗口)中编写了这段代码。

var savepdf = document.querySelector('.savePDF');
savepdf.addEventListener('click', saveaspdf);

function saveaspdf(){
console.log('Inside saveaspdf'); //for checking
browser.tabs.saveAsPDF({footerCenter:"hello",footerLeft:"2",footerRight:"4/10/2017",headerCenter:"Mera Baba",headerLeft:"Baba",headerRight:"Baba",marginBottom:0.5,marginLeft:0.5,marginRight:0.5,marginTop:0.5,orientation:0,paperHeight:11.0,paperSizeUnit:0,paperWidth:8.5,scaling:1,showBackgroundColors:false,showBackgroundImages:false,shrinkToFit:true})
.then((status) => {
console.log(status);
});
}

当我单击该按钮时,出现将其另存为 pdf 的窗口(假设我选择桌面),然后我点击保存。没有任何反应(下载插件也没有变蓝),一个损坏的 pdf 文件被保存到我的桌面。控制台看起来像这样:

result

所以,它进入函数内部,但随后(我不太清楚)“无法发送函数调用结果...”发生了。请帮助我解决这个问题。

这是我的 manifest.json 文件:

"permissions": [
"storage",
"<all_urls>",
"tabs",
"activeTab"
],

"browser_action": {
"default_icon": "icons/pdf.ico",
"default_title": "My pdf",
"default_popup": "popup/addsite.html"
}

编辑:-

我做了一个非常简单的扩展,只包含一个 background.js 文件,并从 this 复制代码地点。尽管如此,该功能似乎起作用的唯一页面是 Firefox 的 about:debugging 页面。所以我不明白我在这里错过了什么?!

最佳答案

browser.tabs.saveAsPDF 只能在后台脚本中运行。您将需要在内容脚本和后台脚本之间进行消息传递。

所以 contentscript.js:

var savepdf = document.querySelector('.savePDF');
savepdf.addEventListener('click', saveaspdf);

function saveaspdf(){
console.log('Inside saveaspdf'); //for checking
browser.runtime.sendMessage("saveCurrentPageAsUrl");
}

背景.js:

browser.runtime.onMessage.addListener(onMessage);

function onMessage(message) {
if(message == "saveCurrentPageAsUrl"){
saveCurrentPageAsUrl();
}
}

function saveCurrentPageAsUrl(){
browser.tabs.saveAsPDF({footerCenter:"hello",footerLeft:"2",footerRight:"4/10/2017",headerCenter:"Mera Baba",headerLeft:"Baba",headerRight:"Baba",marginBottom:0.5,marginLeft:0.5,marginRight:0.5,marginTop:0.5,orientation:0,paperHeight:11.0,paperSizeUnit:0,paperWidth:8.5,scaling:1,showBackgroundColors:false,showBackgroundImages:false,shrinkToFit:true})
.then((status) => {
console.log(status);
});
}

}

影响 Firefox 57 和 Firefox 58 的错误 ( https://bugzilla.mozilla.org/show_bug.cgi?id=1404681 ) 当前阻止大多数页面保存为 PDF,因此应该使用 getBrowserInfo ( https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/getBrowserInfo ) 在插件中内置检查以向用户显示通知不支持时 ( https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/notifications )。

关于javascript - tabs.saveAsPDF() 不工作(Firefox Web-ext),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46564293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com