Reuse iframe in another tab (Javascript) - javascript

I have this iframe in a page: document.querySelector("iframe")
It loads a PDF from a server. to avoid calling another request when the user clicks on a button i want to reuse the iframe with the pdf document from this iframe: ex: <iframe src="myAPI/loadPdf.pdf"><iframe>
When a user clicks a button:
var pdfWindow = window.open("");
console.log(document.querySelector("iframe"));
pdfWindow.document.write(document.querySelector("iframe"));
//to avoid making 2 request, use the one that is already loaded
This writes [object HTMLIFrameElement] on the blank page instead of the document

document.write() writes a string to the document. You want to append the iframe node to document.body:
pdfWindow.document.body.appendChild(document.querySelector('iframe'))
But note that when you append the iframe you can see a request for the pdf file in the network panel, however if the browser could cache the file and the cached version is available then the cached version is used and it's not downloaded again. With that said it's actually no use to reuse the iframe since the same thing happens if you just open the window with the url to the pdf file, the cached version will be used if it's available.

Related

Download a pdf (not open in tab) in Javascript

I have a landing page on which there is a form which is filled by user and then it goes to a thank you page (which is another page). On thank you page I simply want to prompt the user for saving a pdf as the thank you page loads. Please note I want the file to be saved/downloaded and not opened on the same tab or another tab in the browser. I have tried multiple solutions but they all seem to open the pdf in the browser itself and not download it. Some of the methods I have tried are meta tag download, page redirect download, iframe download but they all open the pdf in browser itself and not download it. I just want a simple solution for downloading the file and not opening it in the browser. Also the pdf should download automatically without clicking any link or button.
The document.ready event of the thank you page should trigger a local script that runs a server script to download the PDF. Be sure to set the headers as described above. If you try to link directly to the PDF, it'll try to load in the browser.
Alternatively, you could create the PDF in a directory that is already set to only download files by setting the .htaccess file for that directory.

Run Firefox extension on window.open()

Currently my Firefox add-on uses
require("sdk/tabs").on("ready", runScript);
to attach a script to the document (tab.attach()). But my target website opens a link as a new dialog window (minimal window with no tabs) and my add-on doesn't seem to run on it. How do I make it run on minimal window too?
Usually, you either attach a script to a page when the user presses a button (using tab.attach), which means that the page has already loaded and you don't need to wait for a ready event. Or you attach your script to specific pages based on the URL, in which case you should be using a pagemod which waits for the page to be ready by default.

Chrome extension enable content script to access iframe

Question is simple: Is there any way to enable a content script (through executeScript()) to be able to access an iframe on the current webpage (say stackoverflow.com) that has the same origin of the extension itself?
To put to rest: I realize you can communicate through postMessage, hashing, and so forth from the iframe script to its parent script, but my main goal is to add events onto the iframe directly from a content script rather than having to pass a message (and create a middle man).
I believe injected content scripts run on the page "in their own little world", in which the page itself, nor other content scripts can access, so it leads me to believe Chrome could possibly allow these scripts access to iframes of its own origin (the extension url itself).
EDIT
Just to clarify, the iframe would have a url of the chrome extension itself, under the protocol chrome-extension://. The parent page of the iframe could have any url, say http://stackoverflow.com for instance. So trying to access the iframe from a content script generally wont pass the same-origin-policy...The question is if there is a way around this using Chrome's Extension library.
Thanks!
You can certainly run a content-script on a page and interact with any embedded iframes. I was working on a project recently where I needed to do this.
I don't have a solution in pure javascript so this would require you to include the jQuery.js library, if that works for you.
In the content-script running on the parent page I did the following:
//CAPTURE FRAME LOAD
$("#frameID").load(function () {
//Create an object for the frame
var firstFrame = window.parent.frames[0].document;
//Set JQuery events to run in the context of the frame
$("#buttonID", firstFrame).click(function(){
//Capture click event of a button within the frame
});
});

Is it possible not to allow iframe to show file download popup?

I have a code like:
<iframe src="http://www.upnp.org/download/UPNP_understandingUPNP.doc"></iframe>
When this code is rendered, browser file download prompt is shown to user. I'd want to prohibit iframe from showing this download popup.
Is it possible?
I'd want it as iframe's url is controlled by users, not site owner.
No.
The server hosting the URL can decide if it wants to mark it as an attachment (suggesting that the browser to save it) or inline (suggesting that the browser should open it; using a plugin if one is available).
The browser decides how to handle it.
The page linking to it (even if it does so via an iframe) has no control over any of the above.
To avoid the download popup, you could use ajax instead of an iframe to load the url.

Javascript to Load and Save Files Temporarily

I am working on a thing where I have an iframe. I want to load a static javascript file to this iframe every time user clicks on a button. The javascript file is located at the server and because this is the static file so I would like it to be downloaded to the client once and every time user clicks on the button it just loads this file into iframe without calling server.
I know AJAX can be used to download this file contents and how to add script tag to the iframe but I am not sure where should I temporarily save the contents and then put the contents back into a file to put that file as script tag into iframe.
You can consider this fiddle.
Have you considered just dumping the data into a cookie?

Categories

Resources