Skip to content

Instantly share code, notes, and snippets.

@Laurian
Created October 4, 2009 01:56
Show Gist options
  • Save Laurian/201053 to your computer and use it in GitHub Desktop.
Save Laurian/201053 to your computer and use it in GitHub Desktop.
//
CmdUtils.CreateCommand({
names: ["share command"],
// icon from http://mitcho.com/blog/projects/the-ubiquity-favicon-a-very-important-question/
icon: "http://mitcho.com/blog/wp-content/uploads/2009/06/ubiquibot-favicon3.png",
description: "Share the last Ubiquity command",
help: "How to use your command.",
author: {name: "Laurian Gridinoc", email: "[email protected]"},
license: "GPL",
homepage: "http://namebound.com/ubiquity/share/",
arguments: [{role: 'object', nountype: noun_arb_text}],
preview: function preview(pblock, args) {
var D = CmdUtils.getDocument();
//bit.ly code inspired from http://alexsydell.com/applications/ubiquity/bitly/
var shareUrl = encodeURIComponent(D.shareCommand);
shareUrl = encodeURIComponent("http://namebound.com/ubiquity/share/?" + shareUrl);
var apiUrl = "http://api.bit.ly/shorten?version=2.0.1&longUrl="
+ shareUrl
+ "&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07";
jQuery.getJSON(apiUrl, {}, function(json) {
if (json['results']) {
for (var i in json['results']) {
// Stupid JavaScript, can't figure it out using json['results'][cleanUrl]['shortUrl'].
D.shortShareUrl = json['results'][i]['shortUrl'];
}
pblock.innerHTML = "Last command share URL is <strong>"
+ D.shortShareUrl + "</strong>";
} else {
pblock.innerHTML = "Error shortening the command URL";
}
});
},
execute: function execute(args) {
var D = CmdUtils.getDocument();
D.skipCommand = true;
if (D.shortShareUrl) {
CmdUtils.copyToClipboard(D.shortShareUrl);
displayMessage({icon: this.icon, title: "Copied", text: D.shortShareUrl});
}
}
});
function pageLoad_shareCommand() {
var U = Utils.currentChromeWindow.gUbiquity;
//if (! Utils.xptrService) {
var xptrService = Components.classes["@mozilla.org/xpointer-service;1"].getService();
xptrService = xptrService.QueryInterface(Components.interfaces.nsIXPointerService);
Utils.xptrService = xptrService;
//}
U.msgPanel.addEventListener("popupshown", function() {
var xptrString = xptrService.createXPointerFromSelection(Utils.currentChromeWindow._content.getSelection(), Utils.currentChromeWindow._content.document);
//TODO move xptrString to the doc
Utils.currentChromeWindow.xptrString = xptrString;
}, false);
U.msgPanel.addEventListener("popuphidden", function() {
var D = CmdUtils.getDocument();
// FIXME
if (D.skipCommand) {
D.skipCommand = false;
return;
}
var title = D.title;
if (! D.originalTitle) {
D.originalTitle = title;
}
/*if (title.indexOf("\u2009")) {
title = title.substring(title.indexOf("\u2009") + 1);
}*/
var command = Utils.currentChromeWindow.gUbiquity.textBox.value;
D.title = command + ": " + D.originalTitle;
D.location.hash = 'ubiquity:' + Utils.currentChromeWindow.gUbiquity.textBox.value + ';' + Utils.currentChromeWindow.xptrString;
D.shareCommand = D.location + '';
//save annotation
const AS = (Cc["@mozilla.org/browser/annotation-service;1"].getService(Ci.nsIAnnotationService));
const IOS = (Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService));
var loc = (D.location + '').substring(0, (D.location + '').indexOf('#'));//FIXME
//Utils.log('save anno > ' + loc);
//Utils.log('anno > ' + 'ubiquity/history:' + command);
//Utils.log('v15');
var annotated = false;
AS.getPageAnnotationNames(uri, {}).forEach(function eachAN(annoName) {
//Utils.log('annot: ' + annoName);
if ((annoName.indexOf('ubiquity/history:') == 0)
&& (AS.getPageAnnotation(uri, annoName) == D.location.hash.substring(1))){
annotated = true;
}
});
if (!annotated) AS.setPageAnnotation(IOS.newURI(loc, null, null),
'ubiquity/history:' + Math.random(), D.location.hash.substring(1), 0, 4);
}, false);
var xptrString = CmdUtils.getDocument().location.hash.substring(1);
shareBadge(xptrString);
//load annotations
var D = CmdUtils.getDocument();
const AS = (Cc["@mozilla.org/browser/annotation-service;1"].getService(Ci.nsIAnnotationService));
const IOS = (Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService));
var loc = D.location + '';
if (loc.indexOf('#') != -1) loc = loc.substring(0, loc.indexOf('#'));//FIXME
Utils.log('v17');
//Utils.log('annot? ' + loc);
var uri = IOS.newURI(loc, null, null);
AS.getPageAnnotationNames(uri, {}).forEach(function eachAN(annoName) {
//Utils.log('annot: ' + annoName);
if (annoName.indexOf('ubiquity/history:') == 0) {
var x = AS.getPageAnnotation(uri, annoName);
if (x != xptrString) shareBadge(x);
}
});
}
function shareBadge(xptrString) {
Utils.log('badge ' + xptrString);
if (xptrString.indexOf('#') == 0) xptrString = xptrString.substring(1);
if (Utils.currentChromeWindow._content.document.getElementById(xptrString) != null) return;
var U = Utils.currentChromeWindow.gUbiquity;
var xptrService = Utils.xptrService;
//jump over http://namebound.com/ubiquity/share/?escaped...
var loc = CmdUtils.getDocument().location + '';
if (loc.indexOf('http://namebound.com/ubiquity/share/?') != -1) {
CmdUtils.getDocument().location = unescape(loc.substring(loc.indexOf('?') + 1));
return;
}
//
var command = null;
var id = xptrString;
if (xptrString.indexOf('xpointer') != 0) {
if (xptrString.indexOf('ubiquity:') == 0) {
command = xptrString.substring(9, xptrString.indexOf(';'));
xptrString = xptrString.substring(xptrString.indexOf(';') + 1);
} else {
return;
}
}
var range = xptrService.parseXPointerToRange(xptrString, Utils.currentChromeWindow._content.document);
//TODO empty previous selection
CmdUtils.getWindowInsecure().getSelection().addRange(range);
if (command == null) return;
//insert badge
//stolen from http://azarask.in/verbs/mouse/
var badge = Utils.currentChromeWindow._content.document.createElement("span");
jQuery(badge)
.css({
height:31,
width:26,
backgroundImage: "url(http://azarask.in/verbs/mouse/selection_mark.png)",
position:"absolute",
"-moz-background-inline-policy":"continuous",
cursor:"pointer",
"margin-top": "-20px",
"margin-left": "-10px",
"opacity": .3 })
.attr("id", id)
.attr("title", command)
.mouseup(function(event){
jQuery(badge).remove();
//FIXME: duplicate
var range = xptrService.parseXPointerToRange(xptrString, Utils.currentChromeWindow._content.document);
CmdUtils.getWindowInsecure().getSelection().addRange(range);
U.__cmdManager.updateInput( U.__lastValue = command, U.__makeContext(), function(){});
U.preview(command, true);
});
range.setStart(CmdUtils.getWindowInsecure().getSelection().focusNode, range.endOffset);
xptrService.markElement(badge);
range.insertNode(badge);
//TODO
//jQuery.scrollTo( '#' + id, 800);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment