Created
February 8, 2019 16:54
-
-
Save shalott/c75496e64ceeb0b5edac8ab4164f833f to your computer and use it in GitHub Desktop.
tiny Dreamwidth signal boost bookmarklet
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
var postURL = "https://www.dreamwidth.org/update.bml?event="; | |
var curURL = window.location.href; | |
// get the currently selected text (no longer used in boost) | |
function getSelectionText() { | |
var text = ""; | |
if (window.getSelection) { | |
text = window.getSelection().toString(); | |
} else if (document.selection && document.selection.type != "Control") { | |
text = document.selection.createRange().text; | |
} | |
return text; | |
} | |
// updated, get currently selected html | |
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.innerHTML; | |
} | |
} else if (typeof document.selection != "undefined") { | |
if (document.selection.type == "Text") { | |
html = document.selection.createRange().htmlText; | |
} | |
} | |
return html; | |
} | |
// check if it's locked | |
function isLocked() { | |
return document.getElementsByClassName("access-filter").length > 0; | |
} | |
function confirmBoost() { | |
var doPost = !isLocked(); | |
if (!doPost) { | |
doPost = confirm("Post is access-locked. Continue?"); | |
} | |
return doPost; | |
} | |
// set up the post | |
function boost() { | |
var selectedText = getSelectionHtml(); | |
// name of user who posted | |
var curName = curURL.substr(0, curURL.indexOf('.')); | |
curName = curName.substr(curName.lastIndexOf('/')+1); | |
// title of current post | |
var curTitle = document.title; | |
curTitle = curTitle.substr(curTitle.lastIndexOf(' | ')+3); | |
// build up the content of signal boost optionally using selected text as an excerpt | |
var linkText = "<p>"; | |
linkText = linkText + '<user name="' + curName + '"> posted: '; | |
linkText = linkText + '<strong><a href="' + curURL + '">' + curTitle + '</a></strong>'; | |
linkText = linkText ; | |
if (selectedText.length > 0) { | |
linkText = linkText + "\n<blockquote> " + selectedText + "</blockquote>"; | |
} | |
linkText = linkText + "</p>"; | |
var subjectText = "Signal Boost: " + curTitle; | |
postURL = postURL + encodeURIComponent(linkText) + "&subject=" + encodeURIComponent(subjectText); | |
window.location = postURL; | |
} | |
if (confirmBoost()) { | |
boost(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi! You more muck-around-able code motivated me to make three more changes that had been bugging me:
Since you put in a github link I decided to actually attempt to learn to use my dusty github account! I hope I did it right, it looks like there's no way to do a pull request for a gist but here is my fork (I think): https://gist.github.com/melannen/ac7d03e0255fdfa6086dae63db705dfe
(something seems to have happened to the indents while I was copy-pasting back and forth that confused the version control, sorry. :/ )
Thank you for the fix to the html-stripping! I wouldn't have known where to start on that one.