Fiddle-ify!

Converts code blocks into JSFiddles with the click of a button

2014-08-04 기준 버전입니다. 최신 버전을 확인하세요.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

You will need to install an extension such as Tampermonkey to install this script.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name           Fiddle-ify!
// @author         Cameron Bernhardt (AstroCB)
// @version        0.1.0
// @namespace      http://github.com/CameronBernhardt
// @description    Converts code blocks into JSFiddles with the click of a button
// @include        http://stackoverflow.com/*
// ==/UserScript==

var tags = document.getElementsByClassName("post-taglist")[0].children;
var tagged = false;
var index = 0;
var html, css, javascript; //store contents of selected code blocks

for(var i = 0; i < tags.length; i++){
  var tagName = tags[i].innerHTML;
  if(tagName === "html" || tagName === "css" || tagName === "javascript"){ //check if tagged HTML, CSS, or JavaScript
    tagged = true;
  }
}

if(tagged){ //only display button if tagged HTML, CSS, or JavaScript (TODO: add jQuery support (toggle menu in Fiddle))
  var question = document.getElementsByClassName("vt")[0];
  question.innerHTML += "<br/><button id='fiddleify'>Fiddle-ify!</button>"; //inject "Fiddle-ify" button

  document.getElementById("fiddleify").addEventListener("click", function(){ //listen for "enter" keypresses for skipping
    alert("Click a code block for HTML or press enter to skip.");
    document.addEventListener("keyup", function(e){
      if(e.which === 37){
        e.preventDefault();
        assign(null, index);
        index++;
      }
    });
    var blocks = document.getElementsByClassName("default prettyprint prettyprinted");
    for(var j = 0; j < blocks.length; j++){
      blocks[j].addEventListener("click", function(){ //listen for clicks on code blocks
        assign(this.children[0].children, index);
        index++;
      });
    }
  });
}

function assign(element, num){
  //TODO: add visual cue that code block has been selected
  switch(num){ //instructions for each click: HTML -> CSS -> JavaScript (runs for both "enter" keypresses and block clicks)
    case 0: html = element; alert("Click a code block for CSS or press enter to skip."); break;
    case 1: css = element; alert("Click a code block for JavaScript or press enter to skip."); break;
    case 2: javascript = element; run(); break;
    default: console.log("Finished");
  }
}

function run(){ //unwrap text from code blocks (each word is in its own span)
  confirm("Loading JSFiddle...");
  var htmlText = "", cssText = "", jsText = "";
  for(var x = 0; x < html.length; x++){
    htmlText += html[x].innerHTML;
  }

  for(var y = 0; y < css.length; y++){
    cssText += css[y].innerHTML;
  }

  for(var z = 0; z < javascript.length; z++){
    jsText += javascript[z].innerHTML;
  }

  var fiddleWin = window.open("//jsfiddle.net"); //load Fiddle

  fiddleWin.document.onload = function(){
    var textboxes = fiddleWin.document.getElementsByTagName("textarea");

    //these are dependent on JSFiddle's layout: may need to change if design changes (currently no other way to reference them)
    var htmlWin = textboxes[2];
    var jsWin = textboxes[4];
    var cssWin = textboxes[6];

    htmlWin.innerHTML = htmlText;
    cssWin.innerHTML = cssText;
    jsWin.innerHTML = jsText;
}