javascript function to capture content of the browser window as image

C

Chanchal

Hi,

Any one knows about any java script function which capture the content
of the browser window as image?

Thanks

Chanchal
 
T

Thomas 'PointedEars' Lahn

Chanchal said:
Any one knows about any java script function which capture the content
of the browser window as image?

No, for several reasons, among them:

1. There is no "java script"; Java != JavaScript.

2. There is no single "javascript", but several ECMAScript implementations
(among them, Netscape/Mozilla.org JavaScriptâ„¢ and Microsoft JScript).

3. None of the implementations provides that feature.

4. So, if this would be accomplished with an implementation, a runtime
environment (e.g., a Web browser) would need to provide a corresponding
API (e.g., a DOM) with ECMAScript binding that could be used with the
implementation.

Why do you ask?

Please read <http://jibbering.com/faq/> before you post, and take heed of it
when you reply.


PointedEars
 
S

Stevo

Chanchal said:
Hi,

Any one knows about any java script function which capture the content
of the browser window as image?

Thanks

Chanchal

It can be done in IE via an ActiveX control. Not only can it snapshot
the browser window, it can do the whole desktop.

But in JavaScript, no.
 
C

Chanchal

Apologies for the typo.

I'd like to have this info, because I need to place a link or button
in a JSP page, clicking on which, what is displayed in the browser is
obtained as an image.

Chanchal
 
M

Martin Honnen

Chanchal said:
Any one knows about any java script function which capture the content
of the browser window as image?

With Firefox JavaScript code in an extension can draw the contents of a
window into a canvas:
https://developer.mozilla.org/en/Drawing_Graphics_with_Canvas#Rendering_Web_Content_Into_A_Canvas
And a canvas allows you to export its contents with the toDataURL method
http://www.whatwg.org/specs/web-app.../the-canvas-element.html#dom-canvas-todataurl
I am however not sure whether Firefox implements that method.
 
M

Michael Wojcik

Martin said:
With Firefox JavaScript code in an extension can draw the contents of a
window into a canvas:
https://developer.mozilla.org/en/Drawing_Graphics_with_Canvas#Rendering_Web_Content_Into_A_Canvas

And a canvas allows you to export its contents with the toDataURL method
http://www.whatwg.org/specs/web-app.../the-canvas-element.html#dom-canvas-todataurl

I am however not sure whether Firefox implements that method.

I believe it does, based on the documentation for the ScreenGrab
extension to Firefox, which mentions using the canvas element.
ScreenGrab does something similar to what the OP is asking for, though
of course in a Firefox-specific manner, and using a browser extension
rather than JavaScript.
 
M

Michael Wojcik

Jonathan said:
I'm fairly sure it can't be done. This is because there's no datatype
available that could store such an image.

In ECMAScript? There certainly is - ECMAScript supports byte arrays.
Also, it would open up a security hole - evil.com could place
<img src="http://good.org/secret.img">
on its own page then send the image back to its own server.

Welcome to the world of cross-site scripting. There are already a
great many XSS holes; I don't see offhand how this presents a new one.
 
E

Erwin Moller

Chanchal schreef:
Hi,

Any one knows about any java script function which capture the content
of the browser window as image?

Thanks

Chanchal


Hi,

As others wrote: No.

But a possible way out is this: You can get the complete content of the
document: store it in some variable and send that to some server that
stores it.
Of course you'll only receive the actual source on the server, no media
like images.

Horrible and ugly, but it can be done.

What is it excactly you are trying to accomplish?

Regards,
Erwin Moller


--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
 
M

Martin Honnen

Michael said:
I believe it does, based on the documentation for the ScreenGrab
extension to Firefox, which mentions using the canvas element.

Yes, it does support toDataURL, I have now had time to test with FF 3.0.
ScreenGrab does something similar to what the OP is asking for, though
of course in a Firefox-specific manner, and using a browser extension
rather than JavaScript.

Firefox extensions use JavaScript some I am not sure why you say "rather
than JavaScript".
 
T

Thomas 'PointedEars' Lahn

Michael said:
I believe it does, based on the documentation for the ScreenGrab
extension to Firefox, which mentions using the canvas element.
ScreenGrab does something similar to what the OP is asking for,

Doesn't it do exactly that -- "capture the content of the browser window as
image"?
though of course in a Firefox-specific manner,

No, in a Canvas-specific manner. Or are you referring to the
(content/Screengrab.)XUL that triggers it? That would be
_Gecko_-specific instead.
and using a browser extension rather than JavaScript.

ScreenGrab, like any other extension for Gecko-based browsers, is primarily
written in JavaScript. You may want to take the time and unzip(1)
${installDir}/extensions/{02450954-cdd9-410f-b1da-db804e18c671}/chrome/screengrab.jar
(of ScreenGrab version 0.96.1), where, in content/Browser.js:100, I have
noticed not only

getCanvas: function() {
return document.createElementNS("http://www.w3.org/1999/xhtml",
"html:canvas");
// return document.getElementById("screengrab_buffer_canvas");
},

but also, much to my disappointment, a number of unnecessary eval() calls,
like in content/Grab.js:15 --

screengrab.Grab2 = function(targetName, captureName, action) {
try {
// bring focus to us
screengrab.Browser.contentFrame().focus();
var target = eval("new sg." + targetName + "()");
var capture = eval("sg." + captureName);
target.obtainDimensions(function(browser, dimensions) {
capture(browser, dimensions, function(canvas) {
action(canvas);
});
});
} catch (error) {
screengrab.error(error);
}
}

(sic!) -- which, of course, could and should be rewritten as

screengrab.Grab2 = function(targetName, captureName, action) {
try {
// bring focus to us
screengrab.Browser.contentFrame().focus();
var target = new sg[targetName]();
var capture = sg[captureName];
target.obtainDimensions(
function(browser, dimensions) {
capture(browser, dimensions,
function(canvas) {
action(canvas);
});
});
} catch (error) {
screengrab.error(error);
}
};


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,089
Messages
2,570,602
Members
47,222
Latest member
jspanther

Latest Threads

Top