AcWing content to markdown

将AcWing上的内容转换为markdown

Versione datata 20/03/2022. Vedi la nuova versione l'ultima versione.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name        AcWing content to markdown
// @namespace   acwing
// @match       https://www.acwing.com/*
// @grant       GM_setClipboard
// @version     1.1
// @author      -
// @description 将AcWing上的内容转换为markdown
// @require     https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js
// @require     https://cdn.bootcdn.net/ajax/libs/turndown/7.1.1/turndown.min.js
// @license     MIT
// ==/UserScript==

let turndownService = new TurndownService();
  turndownService.addRule('pre', {
    filter: 'pre',
    replacement: function (content, node) {
      let t = $(node).attr("class").split(/\s+/).slice(-1);
      return "```" + t + "\n" + content.trim() + "\n```";
    }
  });

$("div[data-tab='preview-tab-content']").before(
  "<div> <button class='html2md-view'>显示markdown</button> <button class='html2md-cb'>复制markdown到剪贴板</button> </div>"
);


$(".html2md-cb").click(function() {
  let target = $(this).parent().next().get(0);
  if (!target.markdown)
    target.markdown = turndownService.turndown($(target).html());
  GM_setClipboard(target.markdown);
  // console.log(markdown);
  $(this).text("已复制到剪贴板");
});

$(".html2md-view").click(function() {
  let target = $(this).parent().next().get(0);
  console.log(target);
  if (target.viewmd) {
    target.viewmd = false;
    $(this).text("显示markdown");
    $(target).html(target.original_html);
  } else {
    target.viewmd = true;
    if (!target.original_html)
      target.original_html = $(target).html();
    if (!target.markdown)
      target.markdown = turndownService.turndown($(target).html());
    $(this).text("显示原始内容");
    $(target).html(`<textarea oninput="$(this).parent().get(0).markdown=this.value;" style="width:100%; height:400px;"> ${target.markdown} </textarea>`);
  }
});