Skip to content

Instantly share code, notes, and snippets.

@melannen
Forked from shalott/signal_boost.js
Last active February 12, 2019 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save melannen/ac7d03e0255fdfa6086dae63db705dfe to your computer and use it in GitHub Desktop.
Save melannen/ac7d03e0255fdfa6086dae63db705dfe 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. this checks for a different element now because the previous one inexplicably failed weirdly for a few people. ¯\_(ツ)_/¯
function isLocked() {
return document.getElementsByClassName("security-protected").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();
var subjectText;
// name of user who posted and domain of website
var curName = curURL.substr(0, curURL.indexOf('.'));
curName = curName.substr(curName.lastIndexOf('/')+1);
var siteName = curURL.substr(curName.length+8+Boolean(location.protocol == 'https:'));
siteName = siteName.substr(0, siteName.indexOf('/'));
// title of current post
var curTitle = document.title;
if (siteName=="dreamwidth.org")
{curTitle = curTitle.substr(curTitle.lastIndexOf(' | ')+3);}
// build up the content of signal boost optionally using selected text as an excerpt
var linkText = "<p>";
//determines if user name = syntax will work
if (siteName=="dreamwidth.org")
{linkText = linkText + '<user name="' + curName + '"> posted: ';}
else
{linkText = linkText + 'From <strong><a href="http://' + curName + '.' + siteName + '">' + curName + '.' + siteName + '</a>:</strong> ';}
//builds rest of boost
linkText = linkText + '<strong><a href="' + curURL + '">' + curTitle + '</a></strong>';
linkText = linkText ;
if (selectedText.length > 0) {
linkText = linkText + "\n<blockquote> " + selectedText + "</blockquote>";
}
linkText = linkText + "</p>";
// checks if you are signalboosting a SignalBoost and prevents dangerous vortices from forming
if (!curTitle.includes("Signal Boost: ")){
subjectText = "Signal Boost: " + curTitle;}
else {subjectText = 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