View Single Post
Old 09-21-2009, 04:44 PM   #2
Laurens
Jah Blessed
Laurens is no ebook tyro.Laurens is no ebook tyro.Laurens is no ebook tyro.Laurens is no ebook tyro.Laurens is no ebook tyro.Laurens is no ebook tyro.Laurens is no ebook tyro.Laurens is no ebook tyro.Laurens is no ebook tyro.Laurens is no ebook tyro.
 
Laurens's Avatar
 
Posts: 1,295
Karma: 1373
Join Date: Apr 2003
Location: The Netherlands
Device: iPod Touch
Lightbulb Might not be very difficult to do

I've switched to a Mac and iPod Touch, so no Sunrise for me anymore.

That said, all that the IE and Firefox extensions actually do is invoke sunrisexp.exe with specific command-line parameters to trigger the "clip" functionality. It should be relatively easy to write a similar extension for Chrome or Safari.

The basic outline of what the script should do is:
  1. Obtain the current document's title and URL.
  2. Find the path of the sunrisexp.exe executable in the Windows Registry. (Alternatively, use a hard-coded path, so you can skip this step.)
  3. Invoke sunrisexp.exe with the -clip option followed by the URL and the title.

Example:
Code:
sunrisexp.exe -clip http://www.mobileread.com/ MobileRead
I've attached the Firefox extension's JavaScript code for inspiration. Good luck.

Code:
function clipWithSunriseXp() {
	var url = content.document.URL;
	if (url == "about:blank" || url == null) {
		alert("Cannot clip this page. URL not available.");
		return;
	}
	var title = content.document.title;
	var selectionFile = saveSelection();
	
	var sunrisexp = getSunriseXp();
	if (sunrisexp == null) {
		alert("Could not find Sunrise XP executable.");
		return;
	}
	
	var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
	process.init(sunrisexp);
	
	var args = new Array(3);
	args[0] = "-clip";
	args[1] = title;
	args[2] = url;
	if (selectionFile != "") {
		args[3] = selectionFile;
	}
	process.run(false, args, args.length);
}

function getSunriseXp()
{
	var nsIWindowsRegKey = Components.interfaces.nsIWindowsRegKey;
	var registry = Components.classes["@mozilla.org/windows-registry-key;1"].getService(nsIWindowsRegKey);
	registry.open(nsIWindowsRegKey.ROOT_KEY_LOCAL_MACHINE, "SOFTWARE\\DistantChord\\Sunrise XP", nsIWindowsRegKey.ACCESS_READ);
	var installDir = registry.readStringValue("InstallDir");
	
	var sunrisexp = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
	sunrisexp.initWithPath(installDir + "\\sunrisexp.exe");
	if (sunrisexp.exists()) {
		return sunrisexp;
	} else {
		return null;
	}
}

function saveSelection()
{
	var selection = content.getSelection();
	if (selection.rangeCount == 0) {
		return "";
	}
	
	var range = selection.getRangeAt(0);	
	var span = content.document.createElement("span");
	span.appendChild(range.cloneContents());	
	var html = span.innerHTML;
	
	var file = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties)
		.get("TmpD", Components.interfaces.nsILocalFile);
	file.append("sunrisexp_selection.html");
	var out = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
	out.init(file, 0x02 | 0x08 | 0x20, 0664, 0);
	out.write(html, html.length);
	out.close();
	
	return file.path;	
}
Laurens is offline