Skip to content

Instantly share code, notes, and snippets.

@davidjbeveridge
Last active January 18, 2022 11:32
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidjbeveridge/cdb8484c45ad170a6a88 to your computer and use it in GitHub Desktop.
Save davidjbeveridge/cdb8484c45ad170a6a88 to your computer and use it in GitHub Desktop.

Add to Buffer (in iframe)

Use this handy javascript snippet to add an "Add to Buffer" bookmark. Then you can add anything to your Buffer, anytime, all without leaving the page.

How it works

First, copy the following into your clipboard (it's the attached Javascript, but formatted as a bookmarklet):

javascript:(function()%7Bfunction%20c(a%2Cb)%7BbaseUrl%3D%22%2F%2Fbufferapp.com%2Fadd%3F%22%3Breturn%20baseUrl%2B%22url%3D%22%2Bd(a)%2B%22%26text%3D%22%2Bd(b)%7Dvar%20d%3DencodeURIComponent%3Bc(window.location.href%2Cwindow.document.title)%3Bvar%20a%3Ddocument.createElement(%22iframe%22)%3Ba.setAttribute(%22style%22%2C%22width%3A%20900px%3B%20height%3A%20500px%3B%20position%3A%20fixed%3B%20top%3A%2050%25%3B%20left%3A%2050%25%3B%20margin-left%3A%20-450px%3B%20margin-top%3A%20-250px%3B%20background%3A%20%23fff%3B%20border%3A%20solid%201px%20%23000%3B%20z-index%3A%201000%3B%22)%3Ba.setAttribute(%22frameborder%22%2C%220%22)%3Burl%3Dc(document.location.href%2Cdocument.title)%3Ba.src%3Durl%3Bdocument.body.appendChild(a)%3B%0Adocument.body.appendChild(function(a)%7Bvar%20b%3Ddocument.createElement(%22a%22)%3Bb.setAttribute(%22style%22%2C%22width%3A%2030px%3B%20height%3A%2030px%3B%20position%3A%20fixed%3B%20top%3A%2050%25%3B%20left%3A%2050%25%3B%20margin-left%3A%20-455px%3B%20margin-top%3A%20-255px%3B%20background%3A%20%23000%3B%20color%3A%20%23fff%3B%20font-family%3A%20sans-serif%3B%20font-size%3A%2013px%3B%20font-weight%3A%20bold%3B%20text-align%3A%20center%3B%20line-height%3A%2028px%3B%20z-index%3A%201001%3B%20border%3A%200%20none%3B%20border-radius%3A%20100%25%3B%20cursor%3A%20pointer%3B%20box-shadow%3A%201px%201px%204px%20rgba(0%2C0%2C0%2C0.7)%3B%22)%3Bb.innerHTML%3D%22%5Cu2715%22%3Bb.addEventListener(%22click%22%2Cfunction(c)%7Bc.preventDefault()%3B%0Ac.stopPropagation()%3Bdocument.body.removeChild(a)%3Bdocument.body.removeChild(b)%7D)%3Breturn%20b%7D(a))%7D)()%3B%0A

Then create a new shortcut with that as the URL. Whenever you visit a webpage and you want to add it to your Buffer, just open your handy bookmark!

(function(){
var enc = encodeURIComponent;
function bufferUrl(url, title){
baseUrl = '//bufferapp.com/add?';
return baseUrl+'url='+enc(url)+'&text='+enc(title);
}
function bufferFrame() {
var iframe = document.createElement('iframe');
iframe.setAttribute('style', 'width: 900px; height: 500px; position: fixed; top: 50%; left: 50%; margin-left: -450px; margin-top: -250px; background: #fff; border: solid 1px #000; z-index: 1000;');
iframe.setAttribute('frameborder', '0');
url = bufferUrl(document.location.href, document.title);
iframe.src=url;
return iframe;
}
function closeBtn(iframe) {
var btn = document.createElement('a');
btn.setAttribute('style', 'width: 30px; height: 30px; position: fixed; top: 50%; left: 50%; margin-left: -455px; margin-top: -255px; background: #000; color: #fff; font-family: sans-serif; font-size: 13px; font-weight: bold; text-align: center; line-height: 28px; z-index: 1001; border: 0 none; border-radius: 100%; cursor: pointer; box-shadow: 1px 1px 4px rgba(0,0,0,0.7);');
btn.innerHTML = '\u2715';
btn.addEventListener('click', function(e) {
e.preventDefault();
e.stopPropagation();
document.body.removeChild(iframe);
document.body.removeChild(btn);
});
return btn;
}
(function(iframe) {
document.body.appendChild(iframe);
document.body.appendChild(closeBtn(iframe));
})(bufferFrame(bufferUrl(window.location.href, window.document.title)));
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment