Advertisement
Guest User

xDDDD

a guest
Dec 11th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Copy code from SX
  3. // @namespace    https://gist.github.com/kpym/30d90be41ab5c248cdf7
  4. // @version      0.3
  5. // @description  This script use clipboard.js to add copy button for code sections on SX. When you hover a code the button "</>" appear on the top right corner. Click it and the code is copied.
  6. // @author       Jazgar
  7. // @match        *://*.darkw.pl/*
  8. // @grant        GM_addStyle
  9. // @require https://cdn.rawgit.com/zenorocha/clipboard.js/v1.5.5/dist/clipboard.min.js
  10. // @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
  11. // ==/UserScript==
  12. /* jshint -W097 */
  13.  
  14.  
  15. // ------------------------------------------
  16. // CSS part injected in the page
  17.  
  18.  
  19. // ------------------------------------------
  20. // Code part injected in the page
  21.  
  22. // add the button to any code portion
  23. $('.content').before($('<span class="copy-btn">\\copy{code}</span> - jing')).parent().wrap('<div class="precontainer"></div>');
  24. // do the magic with clipboard.js
  25. new Clipboard('.copy-btn', {
  26.    
  27.     text: function(trigger) {
  28.         var regex = /<br\s*[\/]?>/gi;
  29.         var html = $(trigger.nextElementSibling).html();
  30.         const parser = new DOMParser();
  31.         const htmlDocument = parser.parseFromString(html, "text/html");
  32.  
  33.                 var title = htmlDocument.documentElement.querySelector('span > span > strong').innerHTML;
  34.         var quote = htmlDocument.documentElement.querySelectorAll('blockquote > div');
  35.         var code = htmlDocument.documentElement.querySelectorAll('pre > code');
  36.        
  37.         title = title.replace(regex, "");
  38.      
  39.         var poll = "";
  40.         var quot = "";
  41.         for (i = 0; i < code.length; ++i) {
  42.           poll = poll + "[code]" + code[i].innerHTML + "[/code]";
  43.         }
  44.         quot = quot + "[quote]" + quote[quote.length - 2].innerHTML + quote[quote.length - 1].innerHTML + "[/quote]";
  45.                 quot = quot.replace(regex, "");
  46.  
  47.         alert("[h]" + title + "[/h]\n" + poll + quot);
  48.         return $(trigger.nextElementSibling).text();
  49.     }
  50. })
  51. .on('success',function (e) {  $(e.trigger).html("\\done@copy");
  52.  
  53. })
  54. .on('error',function (e) {$(e.trigger).html("\\error{@*!?}")});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement