Reputation: 4407
So, I got Firefox 56 (Ubuntu Gnome) yesterday and I started experimenting with tabs.saveAsPDF() function (Firefox 56+). So on the site the example that they have shown is for a background script. But I want to trigger it only when I press a button. So I made a button, and wrote this code in the .js
file (a popup).
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);
});
}
When I click the button, the window for saving it as pdf comes(say I select Desktop), and I hit save. Nothing happens(the downloads addon also doesn't turn blue) and a corrupted pdf file is saved to my desktop. The console looks like this :
So, it goes inside the function but then (I don't have much idea) "Cannot send function call result..." happens. Please help me regarding how to solve this.
This is my manifest.json
file :
"permissions": [
"storage",
"<all_urls>",
"tabs",
"activeTab"
],
"browser_action": {
"default_icon": "icons/pdf.ico",
"default_title": "My pdf",
"default_popup": "popup/addsite.html"
}
EDIT :-
I made a very simple extension consisting of only a background.js
file and copied the code from this site. Still then the only page where the function seems to work is the about:debugging
page of Firefox. So I don't understand what am I missing here?!
Upvotes: 1
Views: 494
Reputation: 3704
browser.tabs.saveAsPDF will only work in the background script. You will need messaging between your content script and your background script.
So contentscript.js:
var savepdf = document.querySelector('.savePDF');
savepdf.addEventListener('click', saveaspdf);
function saveaspdf(){
console.log('Inside saveaspdf'); //for checking
browser.runtime.sendMessage("saveCurrentPageAsUrl");
}
background.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);
});
}
}
A bug (https://bugzilla.mozilla.org/show_bug.cgi?id=1404681) affecting Firefox 57 and Firefox 58 is currently preventing most pages from being saved as PDF, therefore a check should be built into the addon using getBrowserInfo (https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/getBrowserInfo) to display a notification to the user when it's not supported (https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/notifications).
Upvotes: 2