View StackOverflow code snippet

Open code snippet on Stack Exchange in CodePen / JSFiddle

  1. // ==UserScript==
  2. // @name View StackOverflow code snippet
  3. // @description Open code snippet on Stack Exchange in CodePen / JSFiddle
  4. // @namespace https://github.com/tiansh/
  5. // @include https://stackoverflow.com/*
  6. // @include https://codegolf.stackexchange.com/*
  7. // @include https://codereview.stackexchange.com/*
  8. // @include https://gamedev.stackexchange.com/*
  9. // @include https://es.stackoverflow.com/*
  10. // @include https://ja.stackoverflow.com/*
  11. // @include https://pt.stackoverflow.com/*
  12. // @include https://ru.stackoverflow.com/*
  13. // @homepageURL https://github.com/tiansh/vsocs
  14. // @supportURL https://github.com/tiansh/vsocs/issues
  15. // @version 1.0
  16. // @license MPL 2.0
  17. // @grant none
  18. // ==/UserScript==
  19.  
  20. // This file is modified from a file of kuma project of Mozilla
  21. // https://github.com/mozilla/kuma/blob/00fc05b101658f863f58d7fc2b1aefecbcf71cf8/kuma/static/js/wiki-samples.js
  22. // The original file is created by Mozilla Foundation and its contributors, and is licensed under MPL 2.0
  23.  
  24. (function(win, doc, $) {
  25. "use strict";
  26.  
  27. var sites = ['codepen', 'jsfiddle'];
  28.  
  29. var sourceURL = win.location.href.split('#')[0];
  30. var plug = '<!-- Content from the Stack Exchange network: ' + sourceURL + ' \n' +
  31. ' User contributions of Stack Exchange licensed under cc by-sa 3.0 with attribution required -->\n\n';
  32.  
  33. var handleSnippet = function () {
  34. $('.snippet-ctas').each(function() {
  35. var $this = $(this);
  36. if ($this.find('.open-in-host-container').length) return;
  37. var $snippet = $this.closest('.snippet');
  38. createSampleButtons($snippet);
  39. });
  40. };
  41. var observer = new MutationObserver(handleSnippet);
  42. observer.observe(document.documentElement, { childList: true, subtree: true });
  43. handleSnippet();
  44.  
  45. function openJSFiddle(title, htmlCode, cssCode, jsCode) {
  46. var $form = $(
  47. '<form target="_blank" method="post" action="https://jsfiddle.net/api/post/library/pure/" style="display:none!important">' +
  48. '<input type="hidden" name="html" />' +
  49. '<input type="hidden" name="css" />' +
  50. '<input type="hidden" name="js" />' +
  51. '<input type="hidden" name="title" />' +
  52. '<input type="hidden" name="wrap" value="b" />' +
  53. '<input type="submit" />' +
  54. '</form>'
  55. ).appendTo(doc.body);
  56.  
  57. $form.find('input[name=html]').val(plug + htmlCode);
  58. $form.find('input[name=css]').val(cssCode);
  59. $form.find('input[name=js]').val(jsCode);
  60. $form.find('input[name=title]').val(title);
  61. $form.get(0).submit();
  62. }
  63.  
  64. function openCodepen(title, htmlCode, cssCode, jsCode) {
  65. var $form = $(
  66. '<form target="_blank" method="post" action="https://codepen.io/pen/define" style="display:none!important">' +
  67. '<input type="hidden" name="data">' +
  68. '<input type="submit" />' +
  69. '</form>'
  70. ).appendTo(doc.body);
  71.  
  72. var data = {
  73. 'title': title,
  74. 'html': plug + htmlCode,
  75. 'css': cssCode,
  76. 'js': jsCode
  77. };
  78. $form.find('input[name=data]').val(JSON.stringify(data));
  79. $form.get(0).submit();
  80. }
  81.  
  82. function openSample(sampleCodeHost, title, htmlCode, cssCode, jsCode) {
  83. var cssCleanCode = cssCode.replace(/\xA0/g, " ");
  84.  
  85. if (sampleCodeHost === 'jsfiddle') {
  86. openJSFiddle(title, htmlCode, cssCleanCode, jsCode);
  87. } else if (sampleCodeHost === 'codepen') {
  88. openCodepen(title, htmlCode, cssCleanCode, jsCode);
  89. }
  90. }
  91. var getButtonText = (function () {
  92. var lang = 'en';
  93. if (location.host === 'es.stackoverflow.com') lang = 'es';
  94. if (location.host === 'ja.stackoverflow.com') lang = 'ja';
  95. if (location.host === 'pt.stackoverflow.com') lang = 'pt';
  96. if (location.host === 'ru.stackoverflow.com') lang = 'ru';
  97. return ({
  98. en: function (host) { return 'Open in ' + host; },
  99. es: function (host) { return 'Abrir en ' + host; },
  100. ja: function (host) { return host + ' で開く'; },
  101. pt: function (host) { return 'Abrir em ' + host; },
  102. ru: function (host) { return 'Открыть на ' + host; },
  103. })[lang];
  104. }());
  105. function createSampleButtons($snippet) {
  106.  
  107. var htmlCode = $snippet.find('.snippet-code-html').text();
  108. var cssCode = $snippet.find('.snippet-code-css').text();
  109. var jsCode = $snippet.find('.snippet-code-js').text();
  110. var title = document.title;
  111.  
  112. var $sampleFrame = $snippet.find('.snippet-ctas');
  113.  
  114. if (htmlCode + cssCode + jsCode === '') return;
  115.  
  116. // add buttons if we have good data
  117. var $buttonContainer = $('<div class="open-in-host-container" />');
  118. $.each(sites, function() {
  119. // convert sitename to lowercase for icon name and host identifier
  120. var sampleCodeHost = this.toLowerCase();
  121. // create button
  122. var $button = $('<button />', { 'class': 'open-in-host' });
  123. // create text
  124. var $text = $('<span />').text(getButtonText(sampleCodeHost));
  125.  
  126. // add button icon and text to DOM
  127. $button.append($text).appendTo($buttonContainer);
  128.  
  129. // add listener for button interactions
  130. $button.on('click', function() {
  131. openSample(sampleCodeHost, title, htmlCode, cssCode, jsCode);
  132. });
  133. });
  134. $buttonContainer.appendTo($sampleFrame);
  135. }
  136. $('<style>').text(
  137. '.open-in-host-container { text-align: right; }' +
  138. '.open-in-host-container > button:not(:last-child) { margin-right: 6px; }' +
  139. '.snippet-code .popout { top: -80px; }'
  140. ).appendTo($('head'));
  141.  
  142. })(window, document, jQuery);

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址