Created
November 25, 2008 14:17
-
-
Save axemclion/28928 to your computer and use it in GitHub Desktop.
Linkify - Ubiquity Command
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This is a command to linkify the selected text with search results in Google | |
Particularily useful to insert links while blogging | |
*/ | |
CmdUtils.CreateCommand( | |
{ | |
name: ["linkify"], | |
icon: "http://axemclion.github.com/favicon.ico", | |
author: | |
{ | |
name: "Parashuram", | |
email: "n.parashuram@yahoo.co.in" | |
}, | |
homepage: "http://axemclion.github.com/#projects/linkify.html", | |
license: "GPL", | |
description: "This is a command to linkify the selected text with 'n'th search result link in Google", | |
help: "This is a command to linkify the selected text with 'n'th search result link in Google. Just select the text to linkify, and give teh search result number as the arguement to insert that link.", | |
currentRange : null, | |
/** | |
* Command variables | |
*/ | |
appId: "dLS1WgbV34HPanHaqoe6u2pJz0KnkMGMIXHhC6ZcdECyhj8e3199HRGbIqWvBXPR", | |
arguments : | |
[ | |
{role: 'object', nountype: noun_arb_text, label: 'query'}, | |
{role: 'instrument', nountype: noun_arb_text, label : 'with'} , | |
{role: 'alias', nountype: noun_arb_text, label : 'as'} | |
], | |
searchResultList: null, | |
searchRequest: null, | |
previewDiv : null, | |
/** | |
* Called when a command is executed | |
* @param {Object} args | |
*/ | |
execute: function(args){ | |
var previewDiv = CmdUtils.getDocument().getElementById("linkifyDiv"); | |
if (previewDiv == null){ | |
previewDiv = CmdUtils.getDocument().createElement("div"); | |
previewDiv.id = "linkifyDiv"; | |
previewDiv.style.width = "50%"; | |
previewDiv.style.top = "0%"; | |
previewDiv.style.left = "25%"; | |
previewDiv.style.zIndex = 99999999; | |
previewDiv.style.position = "fixed"; | |
previewDiv.style.border = "1px SOLID BLACK"; | |
previewDiv.style.background = "WHITE"; | |
CmdUtils.getDocument().body.appendChild(previewDiv); | |
} | |
try { | |
this.currentRange = CmdUtils.getWindow().getSelection(); | |
} | |
catch (e){ | |
} | |
CmdUtils.getDocument().getElementById("linkifyDiv").style.display = ""; | |
previewDiv.innerHTML = "<div style='text-align:left; padding : 10px;'><form id = 'searchForm' onsubmit = 'return false'><input name = 'q' id = 'searchTerm'><input type = 'submit' id = 'query'><a id = 'linkifyClose' href='#' style='float:right'>[close]</a></div><div id = 'searchResults'></div>"; | |
var me = this; | |
CmdUtils.getDocument().getElementById("searchForm").addEventListener("submit", function(event){ | |
me.onSearch(event); | |
}, true); | |
CmdUtils.getDocument().getElementById("linkifyClose").addEventListener("click", function(event){ | |
CmdUtils.getDocument().getElementById("linkifyDiv").style.display = "none"; | |
}, true); | |
CmdUtils.getDocument().getElementById("searchTerm").value = CmdUtils.getSelection(); | |
this.onSearch(); | |
}, | |
onSearch : function(event){ | |
var searchTerm = CmdUtils.getDocument().getElementById("searchTerm").value; | |
var me = this; | |
CmdUtils.getDocument().getElementById("searchResults").innerHTML = "Loading .... "; | |
this.getSearchResults(searchTerm, function(searchResults){ | |
if (searchResults === null){ | |
CmdUtils.getDocument().getElementById("linkifyDiv").style.display = "none"; | |
} | |
CmdUtils.getDocument().getElementById("searchResults").innerHTML =this.renderResults(searchResults); | |
var links = CmdUtils.getDocument().getElementById("searchResults").getElementsByTagName("a"); | |
for (var i = 0; i < links.length; i++){ | |
links[i].addEventListener("click", function(event){ | |
me.onLinkClick(event); | |
}, false); | |
} | |
}); | |
return false; | |
}, | |
onLinkClick : function(event) { | |
//CmdUtils.getWindow().getSelection().addRange(this.currentRange); | |
CmdUtils.setSelection("<a href = '" + event.currentTarget.title + "'>" + CmdUtils.getSelection() + "</a>"); | |
CmdUtils.getDocument().getElementById("linkifyDiv").style.display = "none"; | |
displayMessage("Link : " + event.currentTarget.title); | |
}, | |
/** | |
* Renders the list of search results in the preview mode | |
* @param {Object} searchResultList | |
*/ | |
renderResults: function(searchResultList) | |
{ | |
var searchHTML = "<li style = 'border-bottom : BLACK 1px DASHED'><a href = '#' title = '${link}'><span style = 'font-weight:bold; color : blue; text-decoration:underline'>${title}</span></a><br><span style = 'color:green'>${link}</span><br><span style = 'font-size : 0.9em' >${summary}</span></li>" | |
var resultDiv = ["<div style = 'background-color : #FFFFFF; color : #000000; overflow : scroll; height : 500px'><ol>"]; | |
searchResultList.forEach(function(elem) | |
{ | |
var result = CmdUtils.renderTemplate(searchHTML, | |
{ | |
"title": elem["title"], | |
"summary": elem["abstract"], | |
"link": elem["url"] | |
}); | |
resultDiv.push(result); | |
}); | |
resultDiv.push("</ol></div>") | |
return resultDiv.join(''); | |
}, | |
/** | |
* Gets search results from YAHOO | |
* @param {Object} searchTerm | |
* @param {Object} successCallback | |
*/ | |
getSearchResults: function(searchTerm, successCallback) | |
{ | |
bossUrl = "http://boss.yahooapis.com/ysearch/web/v1/${query}?appid=${appId}&format=${format}" | |
var updateUrl = CmdUtils.renderTemplate(bossUrl, | |
{ | |
"format": "json", | |
"appId": this.appId, | |
"query": searchTerm | |
}); | |
var currentObject = this; | |
this.searchRequest = jQuery.ajax( | |
{ | |
type: "GET", | |
url: updateUrl, | |
dataType: "json", | |
error: function(data) | |
{ | |
displayMessage("Error fetching Search results"); | |
successCallback.call(currentObject, null); | |
}, | |
success: function(data) | |
{ | |
successCallback.call(currentObject, data.ysearchresponse.resultset_web); | |
} | |
}); | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment