diff --git a/embeds/buffer-producthunt.css b/embeds/buffer-producthunt.css new file mode 100644 index 0000000..3755abb --- /dev/null +++ b/embeds/buffer-producthunt.css @@ -0,0 +1,22 @@ +/* The main buffer icon + * + * NOTE - Safari ignores selector specificity with injected stylesheets so + * !important's are needed to override the default producthunt styles + */ +.buffer-producthunt{ + + position: relative; + top: 4px; + width: 16px !important; + height: 16px !important; + background-color: transparent; + background-size: 14px 14px; + /* Inline SVG with the color #CCD6DD - Search in XML for %23CCD6DD to change (%23 === encoded #) */ + background-image: url('data:image/svg+xml;utf8,') !important; + background-repeat: no-repeat; + +} +.buffer-producthunt:hover { + background-image: url('data:image/svg+xml;utf8,') !important; +} + diff --git a/embeds/buffer-producthunt.js b/embeds/buffer-producthunt.js new file mode 100644 index 0000000..c2ef596 --- /dev/null +++ b/embeds/buffer-producthunt.js @@ -0,0 +1,84 @@ +;(function() { + + var config = {}; + config.base = "http://www.producthunt.com"; + config.buttons = [ + { + text: "Add to buffer", + container: 'div.url span:nth-child(2)', + className: 'buffer-producthunt post--product-link', + selector: '.buffer-producthunt', + data: function(elem){ + var titleContainer = $(elem).parents("div.url").children(".post-url.title"); + var title = titleContainer.find("span").text(); + var url = titleContainer.attr("href"); + var subTitle = $(elem).parents("div.url").children(".post-tagline.description").text(); + + return { + text: title + ": " + subTitle, + url: config.base + url, + placement: 'ph-add' + } + } + } + ]; + + var createButton = function (btnConfig) { + var a = document.createElement('a'); + a.setAttribute('class', btnConfig.className); + a.setAttribute('href', '#'); + var i = document.createElement('span'); + i.setAttribute('class', 'icon icon-buffer'); + $(a).append(i); + return a; + }; + + var insertButtons = function () { + + var i, l=config.buttons.length; + for ( i=0 ; i < l; i++ ) { + + var btnConfig = config.buttons[i]; + + $(btnConfig.container).each(function () { + + var container = $(this); + + if ( $(container).hasClass('buffer-inserted') ) return; + + $(container).addClass('buffer-inserted'); + + var btn = createButton(btnConfig); + + $(container).append(btn); + + var getData = btnConfig.data; + + $(btn).click(function (e) { + xt.port.emit("buffer_click", getData(btn)); + e.preventDefault(); + }); + + }); + } + }; + + var removeExtras = function () { + $("span.exclusive-badge .buffer-producthunt-button").remove(); + } + + var productHuntLoop = function(){ + insertButtons(); + removeExtras(); + setTimeout(productHuntLoop, 500); + }; + + // Wait for xt.options to be set + ;(function check() { + if( xt.options && xt.options['buffer.op.producthunt'] === 'producthunt') { + productHuntLoop(); + } else { + setTimeout(check, 2000); + } + }()); +}()); \ No newline at end of file