Skip to content

Instantly share code, notes, and snippets.

@dalerka
Forked from ttscoff/Bullseye.bookmarklet
Last active December 4, 2017 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dalerka/94352a6c45db47c1e38e83b7f6198d3a to your computer and use it in GitHub Desktop.
Save dalerka/94352a6c45db47c1e38e83b7f6198d3a to your computer and use it in GitHub Desktop.
"@Oprium" is a bookmarklet to share the best opportunities from around the web on www.Oprium.com (adapted from Bullseye.js)
javascript:(function(){
var FORUM_URL = "https://oprium.com/new-topic?";
var category = "&category=Opps";
var title = '&title=' + document.title;
// Add Source to 'body' but get only the TLD
var source = window.location.host.split("www.").slice(-1)[0].split('.').slice(-3).join('.')
var body = '&body=Source: [' + source + '](' + window.location.href + ')';
var targetURL = FORUM_URL + category + title + body;
var confirmation = "Share opportunities for good!\n\nDid you select and copy the relevant text?\n\nIf yes, press OK and paste it when a new tab opens.\n\nIf not, Cancel, copy text, and click @Oprium again.\n\nYou can later edit the text in Markdown format and add images.\n\nOriginal source and no duplicates please :)"
if (window.confirm(confirmation)) {
var target = window.open(targetURL); // if this doesn't work use next 2 lines instead:
// var target = window.open(targetURL, '_blank');
// target.location;
}
})();
// Minified version:
javascript:(function(){var n="https://oprium.com/new-topic?&category=Opps"+("&title="+document.title)+("&body=Source: ["+window.location.host.split("www.").slice(-1)[0].split(".").slice(-3).join(".")+"]("+window.location.href+")");if(window.confirm("Share opportunities for good!\n\nDid you select and copy the relevant text?\n\nIf yes, press OK and paste it when a new tab opens.\n\nIf not, Cancel, copy text, and click @Oprium again.\n\nYou can later edit the text in Markdown format and add images.\n\nOriginal source and no duplicates please :)"))window.open(n)})();
// Bookmarklet button:
<a id="bookmarklet" href='javascript:(function(){var n="https://oprium.com/new-topic?&category=Opps"+("&title="+document.title)+("&body=Source: ["+window.location.host.split("www.").slice(-1)[0].split(".").slice(-3).join(".")+"]("+window.location.href+")");if(window.confirm("Share opportunities for good!\n\nDid you select and copy the relevant text?\n\nIf yes, press OK and paste it when a new tab opens.\n\nIf not, Cancel, copy text, and click @Oprium again.\n\nYou can later edit the text in Markdown format and add images.\n\nOriginal source and no duplicates please :)"))window.open(n)})();'>
@Oprium
</a>
javascript:(function(){var p=document.createElement('p');p.innerHTML='<p><strong>Hover over main content & click&hellip;</strong></p><p>then clean up, add tags and post :)</p><br><hr><br><p><strong>Original source and no duplicates please!</strong></p><p>press <i>Esc</i> or <i>Refresh</i> to cancel.</p><small>@Oprium v1.1</small>';p.id='loadingp';p.style='position: fixed; width: 100%; left: 0; top: 0; z-index: 9999999; background: #f9f9f9; color: #bd4f6e; text-align: center; margin: 0; padding: 40px 0; font-size: 16px; font-family: Helvetica, Verdana, sans serif;';document.body.appendChild(p);document.body.appendChild(document.createElement('script')).src='https://cdn.rawgit.com/dalerka/94352a6c45db47c1e38e83b7f6198d3a/raw/3ffc15e6fcb9e5ac6916c9169cb38cdf8eb3b739/@Oprium.js?x='+(Math.random());})();
// "@Oprium" - version 1.1
// This bookmarklet lets you select and share content on Oprium.com
// You can modify it for your own Discourse forum as you wish. Vivat FOSS :)
// Currently uses the code of Bullseye+jQery, Upndown and Rawgit.com as a CDN.
// "Upndown" (~129kb) converts HTML to MD - https://github.com/netgusto/upndown
// Ditched "To-Markdown" (~9kb, but has many issues) - https://github.com/domchristie/to-markdown
// Creates a new topic on a discourse forum via URL:
// http://your.forum/new-topic? title=DOCUMENT_TITLE_TEXT
// &body=MARKDOWN_TEXT &category=category/subcategory &tags=keyword1,keyword2
//
// See: https://meta.discourse.org/t/compose-a-new-pre-filled-topic-via-url/28074
// Bullseye (original code) - https://gist.github.com/ttscoff/6109434
// version 0.3 - Add "Escape" key to cancel
// Only use the first class when constructing path, potentially avoid dynamically-added classnames
// Call the bookmarklet with "window.bullseyeShowFrame=0" at the beginning to return just raw text.
//
// version 0.2 - Resolves relative urls, removes Twitter intent links, tries to update lazy loaded
// image urls, loads from raw source, with fallback for dynamically-loaded content.
// -------------------------------------------------------------------------------------------
// TODO: Get the 'meta keywords' from a page and send them as 'tags=' attribute for a new topic.
(function () {
// You Discourse forum endpoint
var FORUM_URL = "https://oprium.com/new-topic?";
// TODO: consider jQuery replacement: Umbrellajs, Minifiedjs etc.
var DOM_LIB_URL = "https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js";
// Upndown v2.1.0:
var HTML_MD_LIB_URL = "https://cdn.jsdelivr.net/npm/upndown@2.1.0/lib/upndown.bundle.min.js";
// Category to submit to (name or number)
var CATEGORY = "Opps";
// truncate text to avoid app crash due to Nginx URI length limit:
var MAX_LENGTH = 4500; // change or set to '0' (off) as needed.
function submitContent($, title, markdown, showFrame) {
if (MAX_LENGTH > 0 && markdown.length > MAX_LENGTH) {
var ending = '... [See source for more](' + window.location.href + ')';
markdown = markdown.substring(0, MAX_LENGTH - title.length - ending.length) + ending;
}
var $form = $("<form>")
.attr("method", "get")
.attr("action", FORUM_URL)
.append($("<input name=title>").val(title))
.append($("<input name=category>").val(CATEGORY))
.append($("<textarea name=body>").val(markdown)) // Textarea preserves line breaks
.append($("<input name=showframe>").val(showFrame))
.appendTo('body');
$form.submit();
}
function selectContent() {
(function ($) {
var jQuery = $,
$els = $("div,table,article,section,aside");
$('#loadingp').fadeOut("slow", function () {
$(this).remove();
});
$els.on("mouseover mouseout click", function (a) {
a.stopPropagation();
a.type === "mouseover"
? $(this)
.css({outline: "1px solid rgb(194, 130, 148)"})
.addClass("mkhovered")
: $els
.css({outline: "none"})
.removeClass("mkhovered");
if (a.type === "click") {
a.preventDefault();
$(".mkhovered")
.css({outline: "none"})
.removeClass("mkhovered");
var $container = $(this);
var elpath = [],
$parents = $container
.parentsUntil("body")
.add($container);
$parents.each(function (i, n) {
var index = $(n).index();
index = (index < 2)
? ":first"
: ":nth-of-type(" + (index - 1) + ")";
var tag = n
.tagName
.toLowerCase();
if ($(n).attr("id") !== undefined) {
elpath.push("#" + $(n).attr("id"));
} else if (n.className !== "") {
elpath.push(tag + "." + n.className.split(" ")[0] + index);
} else {
elpath.push(tag + index);
}
});
var loadpath = window.location.pathname + " " + elpath.join(" ");
// console.log(loadpath); return;
$("<div>").load(loadpath, function () {
var raw = this;
if ($(raw).text() === "") {
raw = $("<div>")
.append($container.html())
.get(0);
}
$(raw)
.find('a[href*="twitter.com/intent"],a[href*="app.net/intent"]')
.remove();
$(raw)
.find("a,img")
.each(function () {
var el = this,
$el = $(el),
attr = el.tagName === "A"
? "href"
: "src";
if (el.tagName === "IMG") {
if ($el.data("original") !== undefined) {
$el.attr(attr, $el.data("original"));
}
}
if (!/^http/.test($el.attr(attr))) {
if (/^\//.test($el.attr(attr)))
var baselink = window.location.protocol + "//" + window.location.hostname;
else
var baselink = window
.location
.href
.split("/")
.slice(0, -1)
.join("/") + "/";
$el.attr(attr, baselink + $el.attr(attr));
}
});
var showFrame = "1";
if (window.bullseyeShowFrame !== undefined) {
switch (window.bullseyeShowFrame) {
case 0:
case false:
showFrame = "0";
break;
default:
showFrame = "1";
}
}
if (typeof upndown !== undefined) {
var upnd = new upndown();
var title = $('head > title').text();
var bodyHTML = window.location.href + '<br><br>' + raw.innerHTML;
upnd.convert(bodyHTML, function (err, markdown) {
if (err) {
console.err(err);
} else {
submitContent($, title, markdown, showFrame);
}
});
}
});
}
});
$(document).bind("keyup", function (e) {
if (e.keyCode === 27) {
$els.unbind("mouseover mouseout click");
$(".mkhovered")
.css("outline", "none")
.removeClass("mkhovered");
$(document).unbind("keyup");
}
return true;
});
})(jQuery.noConflict(true))
}
// import HTML-Markdown converter
var htmd = document.createElement("script");
htmd.src = HTML_MD_LIB_URL;
document.body.appendChild(htmd);
// import jQuery or alternative
var s = document.createElement("script");
s.src = DOM_LIB_URL;
if (s.addEventListener) {
s.addEventListener("load", selectContent, false);
} else if (s.readyState) {
s.onreadystatechange = selectContent;
}
document.body.appendChild(s);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment