Skip to content

Instantly share code, notes, and snippets.

@markharwood
Created December 6, 2013 10:50
Show Gist options
  • Save markharwood/7821876 to your computer and use it in GitHub Desktop.
Save markharwood/7821876 to your computer and use it in GitHub Desktop.
A "bookmarklet" to provide del.icio.us-style saving of webpage content into a search index
javascript:
function url_domain(url){
var a=document.createElement('a');
a.href=url;
return a.hostname;
}
function lowerCaseString(s){
if(s){
return s.toLowerCase();
}
return "";
}
var esDoc={};
var mhl=document.links;
var ds={};
for(var i=0;i<mhl.length;i++){
var l=mhl[i];
var d=url_domain(l.href);
if((d.length>0)&&(l.text.length>0)){
var dls=ds[d];
if(!dls){
dls=[];
ds[d]=dls;
}
dls.push(l.text);
}
}
var canonicalUrl="";
var mhl=document.getElementsByTagName("link");
for(var i=0;i<mhl.length;i++){
if(mhl[i].getAttribute("rel")==="canonical"){
canonicalUrl=mhl[i].getAttribute("href");
console.log("canon="+canonicalUrl);
break;
}
}
esDoc.description=window.title;
var mhl=document.getElementsByTagName("meta");
for(var i=0;i<mhl.length;i++){
var prop=lowerCaseString(mhl[i].getAttribute("property"));
var name=lowerCaseString(mhl[i].getAttribute("name"));
if(prop==="og:url"){
canonicalUrl=mhl[i].getAttribute("content");
}
if(prop==="og:description"){
esDoc.description=mhl[i].getAttribute("content");
}
if(name==="description"){
esDoc.description=mhl[i].getAttribute("content");
}
if(prop==="og:image"){
esDoc.image=mhl[i].getAttribute("content");
}
}
if(canonicalUrl.length==0){
canonicalUrl=window.location;
}
var note=prompt("Save note",document.title);
if(note){
esDoc.title=note;
esDoc.outboundDomainLinks=[];
for(url in ds){
esDoc.outboundDomainLinks.push(url);
}
var x=new XMLHttpRequest();
//Avoid any contamination of array behaviour by the current web page- see http://stackoverflow.com/questions/710586/json-stringify-bizarreness
if(window.Prototype) {
//Caution!! The "compress" function in this bookmarket builder (http://subsimple.com/bookmarklets/jsbuilder.htm ) loses the
// space between "delete" and "Array" in the line below and needs fixing
delete Array.prototype.toJSON;
}
var string=JSON.stringify(esDoc);
x.open('POST',"http://localhost:9200/bookmarks/bookmark/"+encodeURIComponent(canonicalUrl),true);
x.setRequestHeader("Content-type","application/x-www-form-urlencoded");
x.send(string);
}
javascript:function url_domain(url){var a=document.createElement('a');a.href=url;return a.hostname;}function lowerCaseString(s){if(s){return s.toLowerCase();}return "";}var esDoc={};var mhl=document.links;var ds={};for(var i=0;i<mhl.length;i++){var l=mhl[i];var d=url_domain(l.href);if((d.length>0)&&(l.text.length>0)){var dls=ds[d];if(!dls){dls=[];ds[d]=dls;}dls.push(l.text);}}var canonicalUrl="";var mhl=document.getElementsByTagName("link");for(var i=0;i<mhl.length;i++){if(mhl[i].getAttribute("rel")==="canonical"){canonicalUrl=mhl[i].getAttribute("href");console.log("canon="+canonicalUrl);break;}}esDoc.description=window.title;var mhl=document.getElementsByTagName("meta");for(var i=0;i<mhl.length;i++){var prop=lowerCaseString(mhl[i].getAttribute("property"));var name=lowerCaseString(mhl[i].getAttribute("name"));if(prop==="og:url"){canonicalUrl=mhl[i].getAttribute("content");}if(prop==="og:description"){esDoc.description=mhl[i].getAttribute("content");}if(name==="description"){esDoc.description=mhl[i].getAttribute("content");}if(prop==="og:image"){esDoc.image=mhl[i].getAttribute("content");}}if(canonicalUrl.length==0){canonicalUrl=window.location;}var note=prompt("Save note",document.title);if(note){esDoc.title=note;esDoc.outboundDomainLinks=[];for(url in ds){esDoc.outboundDomainLinks.push(url);}var x=new XMLHttpRequest();if(window.Prototype){delete Array.prototype.toJSON;}var string=JSON.stringify(esDoc);x.open('POST',"http://localhost:9200/bookmarks/bookmark/"+encodeURIComponent(canonicalUrl),true);x.setRequestHeader("Content-type","application/x-www-form-urlencoded");x.send(string);}
# = Create a fresh index
curl -X DELETE "http://localhost:9200/bookmarks"
curl -X PUT "http://localhost:9200/bookmarks"
curl -X PUT "http://localhost:9200/bookmarks/bookmark/_mapping" -d '
{
"bookmark": {
"properties": {
"title": {
"type": "string"
},
"outboundDomainLinks": {
"type": "string",
"index":"not_analyzed"
},
"description": {
"type": "string"
},
"image": {
"type": "string",
"index":"no"
}
},
"_timestamp" : { "enabled" : true }
}
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment