Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Copy code from SX
- // @namespace https://gist.github.com/kpym/30d90be41ab5c248cdf7
- // @version 0.3
- // @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.
- // @author Jazgar
- // @match *://*.darkw.pl/*
- // @grant GM_addStyle
- // @require https://cdn.rawgit.com/zenorocha/clipboard.js/v1.5.5/dist/clipboard.min.js
- // @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
- // ==/UserScript==
- /* jshint -W097 */
- // ------------------------------------------
- // CSS part injected in the page
- // ------------------------------------------
- // Code part injected in the page
- // add the button to any code portion
- $('.content').before($('<span class="copy-btn">\\copy{code}</span> - jing')).parent().wrap('<div class="precontainer"></div>');
- // do the magic with clipboard.js
- new Clipboard('.copy-btn', {
- text: function(trigger) {
- var regex = /<br\s*[\/]?>/gi;
- var html = $(trigger.nextElementSibling).html();
- const parser = new DOMParser();
- const htmlDocument = parser.parseFromString(html, "text/html");
- var title = htmlDocument.documentElement.querySelector('span > span > strong').innerHTML;
- var quote = htmlDocument.documentElement.querySelectorAll('blockquote > div');
- var code = htmlDocument.documentElement.querySelectorAll('pre > code');
- title = title.replace(regex, "");
- var poll = "";
- var quot = "";
- for (i = 0; i < code.length; ++i) {
- poll = poll + "[code]" + code[i].innerHTML + "[/code]";
- }
- quot = quot + "[quote]" + quote[quote.length - 2].innerHTML + quote[quote.length - 1].innerHTML + "[/quote]";
- quot = quot.replace(regex, "");
- var result = "[h]" + title + "[/h]" +
- "\n\n[h]» Dane techniczne: «[/h]\n" +
- quot +
- "\n\n[h]» Download: «[/h]\n" +
- poll;
- //alert(result);
- return result;
- }
- })
- .on('success',function (e) { $(e.trigger).html("\\done@copy");
- })
- .on('error',function (e) {$(e.trigger).html("\\error{@*!?}")});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement