Reputation: 11
I have a problem getting my form submission to run in a firefox extension. Here is the relevant part of my code:
function downloadfile(Fname,content) {
form = '<form action="'+_currhost+'/cgi/returnasfile.php" method="post" autocomplete="no">'
+' <input type="text" id="fname" name="fname">'
+' <textarea id="content" name="content" ></textarea>'
+' <button type="submit" id="submit" name="submit"></button>'
+' </form>'
$('#dldiv').html(form);
$('#fname').val(Fname);
$('#content').val(content);
$('#submit').click();
$('#dldiv').html('');
}
It should just return the content and the filename to open the download window from the browser. The php-script only return the content with the adequate header:
$content = (array_key_exists('content', $_POST)) ? $_POST['content'] : '';
$fname = (array_key_exists('fname', $_POST)) ? $_POST['fname'] : '';
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="'.$fname.'"');
echo $content;
But it only opens a window with this content: screenshot
when I do this directly with this extensionpage in a standard webpage directly on my server its running well. Also in Chrome it runs perfectly as extension and standard page. Are there any special permissions necessary in the manifest? I have enabled those:
"permissions": [
"activeTab",
"contextMenus",
"storage",
"http://*/",
"https://*/",
"file://*/*"
],
My firefox Version is 58.0.1 (64-bit) on a debian sid
Similar question have been asked before. But I didn't find anything how to solve this in an extension. And its working in "normal" mode for me.
Thanks and regards
Manfred
Upvotes: 0
Views: 637
Reputation: 11
Just to share my new code, which worked now because I worked around the problem with the POST data. Maybe it has something to do with this bug: Can't submit form to an http(s) URL using POST method from WebExtensions
Here is now my workaround (tested in firefox native page and webextension and in Chrome native page and webextension)
function downloadfile(filename,content) {
if (typeof browser !== 'undefined'){
browser.downloads.download({
url: URL.createObjectURL(new Blob([ content ])),
filename: filename,
saveAs: true,
})
} else{
form = '<form id="dlfrm" name="dlfrm" action="'+_currhost+'/cgi/returnasfile.php" method="post" autocomplete="no">'
+' <input type="text" id="fname" name="fname">'
+' <textarea id="content" name="content" ></textarea>'
+' <button type="button" id="btsubmit" name="btsubmit"></button>'
+' </form>'
$('#dldiv').html(form);
$('#fname').val(filename);
$('#content').val(content);
$('#btsubmit').on('click',function(){
$('#dlfrm').submit();
})
$('#btsubmit').click();
$('#dldiv').html('');
}
}
Upvotes: 1