Last active
August 29, 2015 13:57
-
-
Save alecperkins/9496460 to your computer and use it in GitHub Desktop.
Bookmarklet for scraps.alecperkins.net that saves the selected text as an entry.
This file contains hidden or 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
(function(){ | |
var CONTENT_API_ROOT = 'http://marquee.by/content/'; | |
var CONTENT_API_TOKEN = '<token>'; | |
function getSelectionHtml() { | |
var html = ""; | |
if (typeof window.getSelection != "undefined") { | |
var sel = window.getSelection(); | |
if (sel.rangeCount) { | |
var container = document.createElement("div"); | |
for (var i = 0, len = sel.rangeCount; i < len; ++i) { | |
container.appendChild(sel.getRangeAt(i).cloneContents()); | |
} | |
html = container.innerText; | |
} | |
} | |
return html; | |
} | |
function makeSlug() { | |
var d = new Date(); | |
function _pad(v) { | |
if(v<10){ | |
return "0" + v.toString(); | |
} | |
return v.toString(); | |
} | |
return d.getUTCFullYear().toString() + _pad(d.getUTCMonth() + 1) + _pad(d.getUTCDate()) + _pad(d.getUTCHours()) + _pad(d.getUTCMinutes()) + _pad(d.getUTCSeconds()) + 'z'; | |
} | |
function submitScrap(text, link, location) { | |
var additional_text = prompt('Additional thoughts?'); | |
if(!additional_text) { | |
additional_text = ''; | |
} else { | |
additional_text += '\n\n'; | |
} | |
var content = '> ' + text; | |
content = additional_text + content.replace(/\n/g, '\n>\n> '); | |
var scrap_data = { | |
type: 'text', | |
role: 'paragraph', | |
scrap: true, | |
content: content, | |
is_listed: true, | |
link: link, | |
slug: makeSlug(), | |
is_clipping: true, | |
location: location, | |
clipping_text: text, | |
clipping_link: link, | |
clipping_title: document.title, | |
title: '' | |
} | |
console.log(JSON.stringify(scrap_data)); | |
var xhr = new XMLHttpRequest(); | |
xhr.open('POST', CONTENT_API_ROOT, true); | |
xhr.setRequestHeader('Content-Type', 'application/json'); | |
xhr.setRequestHeader('Authorization', 'Token ' + CONTENT_API_TOKEN); | |
xhr.onload = function () { | |
alert('Saved scrap'); | |
console.log(xhr.responseText); | |
} | |
xhr.send(JSON.stringify(scrap_data)); | |
} | |
var text_to_save = getSelectionHtml(); | |
var link_to_save = window.location.href; | |
if(text_to_save) { | |
if(confirm('Include location?')) { | |
navigator.geolocation.getCurrentPosition(function(pos){ | |
submitScrap(text_to_save, link_to_save, { | |
lat: pos.coords.latitude, | |
lon: pos.coords.longitude | |
}) | |
}) | |
} else { | |
submitScrap(text_to_save, link_to_save, null); | |
} | |
} else { | |
alert('No text selected'); | |
} | |
})(); |
This file contains hidden or 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
javascript:%20(function%20(s)%20%7Bs%20%3D%20document.createElement(%27script%27)%3Bs.src%20%3D%20%27http://scraps.alecperkins.net/bookmarklet.js%3Ftoken=%3D<token>%27%3Bdocument.body.appendChild(s)%7D)()%3B |
This file contains hidden or 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
javascript: (function (s) { | |
s = document.createElement('script'); | |
s.src = 'http://scraps.alecperkins.net/bookmarklet.js?token=<token>'; | |
document.body.appendChild(s) | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment