Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bookmarklet: show current url posted on Reddit
// Create bookmarklet with: https://ted.mielczarek.org/code/mozilla/bookmarklet.html
var getPermalink = post => 'https://www.reddit.com'+post.data.permalink;
var getPermalinks = r => r.data.children.map(getPermalink);
var buildLink = url => {
var a = document.createElement('a');
a.href = url;
a.textContent = url.match(/\/r\/[^/]+/) && url.match(/\/r\/[^/]+/)[0] || url;
a.style.display = 'block';
return a;
};
var buildLinks = urls => urls.map(buildLink);
var addLink = link => document.body.insertBefore(link, document.body.firstChild);
var log = a => console.log(a);
window.redditCalled = r => {
var links = buildLinks(getPermalinks(r));
if(!links.length) {
alert('Current page not found on Reddit');
return;
}
var overlay = document.createElement('div');
overlay.style = 'position: fixed; z-index: 999999; top: 0; left: 0; max-width: 100%; padding: 1em; background: #fff;';
links.map(link => overlay.appendChild(link));
var button = document.createElement('button');
button.textContent = 'Close';
button.onclick = e => document.body.removeChild(overlay);
overlay.appendChild(button);
document.body.appendChild(overlay);
window.scroll(0, 0);
};
var s = document.createElement('script');
s.src = 'https://www.reddit.com/api/info.json?jsonp=redditCalled&url='+encodeURIComponent(location.href);
document.head.appendChild(s);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.