Skip to content

Instantly share code, notes, and snippets.

@shalott
Created February 8, 2019 16:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save shalott/c75496e64ceeb0b5edac8ab4164f833f to your computer and use it in GitHub Desktop.
Save shalott/c75496e64ceeb0b5edac8ab4164f833f to your computer and use it in GitHub Desktop.
tiny Dreamwidth signal boost bookmarklet
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();
}
@melannen
Copy link

melannen commented Feb 9, 2019

Hi! You more muck-around-able code motivated me to make three more changes that had been bugging me:

  1. I changed the method of checking for a locked post for one that seemed to be working for a few people that had problems (possibly custom-site-style-related?) with my first version.
  2. I made it fail more gracefully with non-DW posts.
  3. I put in a check to prevent space-time vortices.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment