Skip to content

Instantly share code, notes, and snippets.

@paulroub
Last active October 9, 2015 23:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save paulroub/c87f30e2aaad9cbdec6d to your computer and use it in GitHub Desktop.
Save paulroub/c87f30e2aaad9cbdec6d to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name CodePen from Snippet
// @namespace http://roub.net/cpfs
// @version 0.3
// @description adds a button to SO snippets to forward the snippet data to CodePen
// @author Paul Roub <paul@roub.net>
// @match http://stackoverflow.com/questions/*
// @grant none
// ==/UserScript==
function setUpButtons() {
$('.copySnippet').each(
function() {
var btn = $(this);
var ctr = btn.closest('.snippet-code');
var html = ctr.children('.snippet-code-html').text();
var js = ctr.children('.snippet-code-js').text();
var css = ctr.children('.snippet-code-css').text();
var datastr = JSON.stringify(
{
'html': html,
'js': js,
'css': css,
'description': "Source: " + document.location.href,
'title': "Snippet from " + document.title
}
);
var form = $('<form action="http://codepen.io/pen/define" method="POST" target="_blank" style="display: inline-block">');
$('<input type="hidden" name="data">').
val(datastr).
appendTo(form);
$('<input class="cpSnippet" type="submit" value="Copy snippet to CodePen">').
attr('style', btn.attr('style')).
appendTo(form);
form.insertAfter(btn);
}
)
}
setTimeout( setUpButtons, 1000 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment