7sIV799F

Untitled

Apr 8th, 2015
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name       2ch code embedded
  3. // @version    0.3.1
  4. // @match      https://2ch.hk/*
  5. // @grant      none
  6. // ==/UserScript==
  7.  
  8. $(document).ready(getIframe);
  9.  
  10. $('.posts').bind('DOMSubtreeModified', getIframe);
  11.  
  12. function getIframe() {
  13.   var re = /(pastebin\.com|ideone\.com|jsfiddle\.net|codepen\.io)\/(\w+\/)?(\w+\/pen\/)?(\w+)\/?$/;
  14.  
  15.   $('a[href^="http"')
  16.     .filter(function() {
  17.       return !$(this).hasClass('code-embedded') && re.test(this.href);
  18.     })
  19.     .addClass('code-embedded')
  20.     .after(' <button class="embed-code">code</button>')
  21.     .next('button.embed-code')
  22.       .click(function() {
  23.         var $this = $(this),
  24.             match = $this.prev().attr('href').match(re);
  25.  
  26.         if (!match) return;
  27.  
  28.         var host = match[1],
  29.             id   = match[4],
  30.             src  = {
  31.               'pastebin.com': '//pastebin.com/embed_iframe.php?i=' + id,
  32.               'ideone.com'  : '//ideone.com/embed/' + id,
  33.               'jsfiddle.net': '//jsfiddle.net/' + id + '/embedded/',
  34.               'codepen.io'  : '//codepen.io/brianknapp/embed/' + id + '/?height=500'
  35.             };
  36.  
  37.         $('<div>', {
  38.           class: 'code-embedded',
  39.           html: $('<iframe>', {
  40.             src: src[host],
  41.             style: 'border:none; width:800px; height:500px'
  42.           })
  43.         }).insertAfter(this);
  44.  
  45.         $this.remove();
  46.       });
  47. }
Advertisement
Add Comment
Please, Sign In to add comment