Pearl Crescent
Page Saver Toolkit Documentation |
Pearl Crescent Page Saver Toolkit
Documentation
Overview
The Pearl Crescent Page Saver Toolkit is an extension for
Mozilla Firefox that provides APIs to allow you to leverage the page capture
capabilities of Page Saver Pro.
Here are some examples of what you can do with the Toolkit:
Before you can use the Toolkit, you must install it in Firefox. You must also install Page Saver Pro 2.5 or newer. For more information about installing extensions, please refer to these instructions (from AccessFirefox.org). Capturing Images
from a Web Page
To make it easier to use the API, we created a small JavaScript file
named pagesaver-toolkit.js which you may
download and use in your own web pages. The examples presented in
this section use this file.
Enabling Web Page Access
to the API
For security reasons, unprivileged JavaScript code in a web page is not able
to use the Page Saver API by default.
It should only be enabled if you are operating Firefox within a controlled
environment (for example, if it is being used for automated quality assurance
testing on computers that are protected by a firewall).
Initializing the API
The main Page Saver Toolkit API call is the
PageSaverCaptureImage()
function, which may be called after the API is initialized. To
initialize the API, send a PageSaverInitAPI
event to the window, or use the
PageSaverInitialize() function that is
included in pagesaver-toolkit.js. Example 1 shows how to call
PageSaverInitialize().
<script language="JavaScript" src="pagesaver-toolkit.js" type="text/javascript"></script> <script type="text/javascript"> var havePSAPI = PageSaverInitialize(); if (!havePSAPI) alert("Sorry. The Page Saver API is not available."); </script> Initialization needs to be done only once. Capturing a Page
To capture an image of a page, call the
PageSaverCaptureImage() function.
You should not call PageSaverCaptureImage()
function a second time until the previous page capture operation has
finished. Refer to the Using a
Completion Function section to learn how to get notified when an
operation has finished.
Example 2 is an HTML page that captures an image of its entire contents after it is loaded and saves the result as a JPEG file. <html> <head> <title>Page Saver Toolkit — Example 2</title> <script language="JavaScript" src="pagesaver-toolkit.js" type="text/javascript"></script> <script type="text/javascript"> function SaveAsJPEG() { if (!PageSaverInitialize()) alert("Sorry. The Page Saver API is not available"); else { var options = "noInteraction,format=image/jpeg@quality=90"; PageSaverCaptureImage(null, kPageSaverPortionEntire, kPageSaverDestinationLocalFile, null, options, null); } } </script> </head> <body onload="setTimeout('SaveAsJPEG();', 0);"> This page contains an onload handler that saves an image of this page to a file. <p>The file will be saved to Page Saver Pro's default file location. </body> </html> PageSaverCaptureImage()
Details
The PageSaverCaptureImage() function offers
a high level of control over the content being captured. Its six
parameters are:
Using a
Completion Function
By passing the name of a completion function to the
PageSaverCaptureImage function, you can
receive status information when each page capture operation finishes.
Example 3 demonstrates use of a completion function, as well as capturing the contents of an embedded iframe window. Since the destination is the system clipboard, this example requires Mac OS or Windows. <html> <head> <title>Page Saver Toolkit — Example 3</title> <script language="JavaScript" src="pagesaver-toolkit.js" type="text/javascript"></script> <script type="text/javascript"> function CapturePage(aDestination) { if (!PageSaverInitialize()) { SetStatusText("The Page Saver API is not available"); return; } var frameWindow = document.getElementById("theFrame").contentWindow; PageSaverCaptureImage(frameWindow, kPageSaverPortionVisible, aDestination, null, null, "OnCaptureComplete"); SetStatusText("capturing..."); } function OnCaptureComplete(aResultCode, aMsg) { var text; if (aResultCode != kPageSaverResultCodeSuccess) { text = "capture failed: " + aResultCode; if (aMsg) text += " (" + aMsg + ")"; } else text = "clipboard copy done."; SetStatusText(text); } function SetStatusText(aText) { var e = document.getElementById("statusText"); if (e) e.innerHTML = aText; } </script> </head> <body> Click the button below to copy an image of the visible portion of the iframe below to the system clipboard. <p> <form> <input type="button" value="Copy to Clipboard" onclick="CapturePage(kPageSaverDestinationClipboard)"> Status: <span id="statusText">Ready</span> </form> <p> <div style="margin-bottom: 5px;"> Site: https://pearlcrescent.com/products/ </div> <iframe id="theFrame" height="600" width="925" src="https://pearlcrescent.com/products/"> </body> </html Capturing Images
from a Firefox Extension
The Page Saver Toolkit also allows privileged (“chrome”) JavaScript
or C++ code to capture images of web pages. This capability allows you
to write a Firefox extension for private use that builds on the features
provided by Page Saver Pro.
Getting
Started
The Page Saver Toolkit extension provides an
XPCOM component
that implements an interface named
pagesaverICapturePageImage.
To use Page Saver API, first create an instance of the component as shown
in Example 4.
var cpiObj = Components.classes["@pearlcrescent.com/PSCapturePageImage;1"] .createInstance(Components.interfaces.pagesaverICapturePageImage);The pagesaverICapturePageImage interface includes a readonly attribute named canCapture, a method named capturePageImage(), and several constants for use in calls to capturePageImage(). You can download or view the interface definition: Using the canCapture
Attribute
The canCapture attribute contains a Boolean
value that is true if page capture
capabilities are available. For example:
var cpiObj = Components.classes["@pearlcrescent.com/PSCapturePageImage;1"] .createInstance(Components.interfaces.pagesaverICapturePageImage); if (cpiObj.canCapture) { // Page Saver Pro is installed and ready. } else { // Page Saver Pro is not installed. } Using the
capturePageImage() Method
Call the capturePageImage() method to
initiate a page capture operation, a portion of which will take place in the
background (asynchronously). This method accepts five parameters and
is defined as:
void capturePageImage(in nsIDOMWindow aContentWindow, in short aCapturePortion, in short aDestination, in nsISupports aDestObj, in string aOptions);Please refer to the interface definition for a detailed description of each parameter. Like most XPCOM methods, capturePageImage() may throw exceptions (for example, if you pass it an invalid parameter). You should not call capturePageImage() a second time until the previous page capture operation has finished. Refer to the next section, Receiving Notifications, to learn how to be notified when an operation has finished. Example 6 shows how to use capturePageImage(). The MyPageCapture() function shown might be called from a browser overlay to save an image of the active page to the local file identified by the aDestinationPath parameter. function MyPageCapture(aDestinationPath) { var cpiObj = Components.classes["@pearlcrescent.com/PSCapturePageImage;1"] .createInstance(Components.interfaces.pagesaverICapturePageImage); if (cpiObj.canCapture) { try { var localFile = Components.classes["@mozilla.org/file/local;1"] .createInstance(Components.interfaces.nsILocalFile); localFile.initWithPath(aDestinationPath); var options = "noInteraction,format=image/png@transparency=none"; cpiObj.capturePageImage(document.contentWindow, cpiObj.PORTION_ENTIRE, cpiObj.DESTINATION_LOCALFILE, localFile, options); } catch (e) {} } } Receiving
Notifications
When a page capture operation completes, the Page Saver Toolkit broadcasts
a message with the topic
PageSaver:CaptureComplete that may be
received using an observer. The subject of the notification will
be the window whose contents were captured and the data string will
contain a numeric status code followed by an optional text message.
If a text message is present, it will follow the numeric status code and a
- character.
The text message contains extra information that may be helpful in debugging failures. In addition, when a capture was successful but the page was too large to capture in its entirety, it will contain the text imagecropped. For more information about image capture size limitations, see the section Known Issues and Limitations within the Page Saver documentation. Example 7 shows how to install a PageSaver:CaptureComplete observer. var myObserver = { observe: function(aSubject, aTopic, aData) { if (aTopic == "PageSaver:CaptureComplete") { var theWindow = aSubject; var rc = -1; if (aData != null && aData.length > 0) rc = parseInt(aData); var textIndex = aData ? aData.indexOf('-') : -1; var text = (textIndex > 0) ? aData.substr(textIndex + 1) : null; alert("Page Saver Capture done:\n" + "window: " + theWindow + "\n" + "resultCode: " + rc + "\n" + "text: " + text + "\n"); } } }; ... var obsSvc = Components.classes["@mozilla.org/observer-service;1"] .getService(Components.interfaces.nsIObserverService); obsSvc.addObserver(myObserver, "PageSaver:CaptureComplete", false); Troubleshooting
If you encounter an unexpected error, please follow these suggestions:
Additional Assistance
If you need more help or would like to send us a suggestion, please
Contact Us.
Other Information
Page Saver Toolkit Extension GUID:
e3aedca3-9871-4dc3-9be8-e175c1513616
Authors and Contributors
|
Subscribe to our announcement list. |