User:Liouxiao/test2.js

維基文庫,自由的圖書館

注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的变更的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5Ctrl-R(Mac为⌘-R
  • Google Chrome:Ctrl-Shift-R(Mac为⌘-Shift-R
  • Internet Explorer或Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5
  • Opera:Ctrl-F5
//adjust vertical paragraph styles
var backup_content = "";
var optimized_content = "";
var optimized_done = false;
var target_div;
var isFirefox = (navigator.userAgent.toLowerCase().indexOf('firefox') !== -1) ? true : false;
var opening_quote_margin_left = "-9px;";
if (isFirefox) {
 opening_quote_margin_left = "-5px;";
}

var paragraphs = document.querySelectorAll("div#mw-content-text div.mw-parser-output");
if (paragraphs.length > 0) {
  backup_content = paragraphs[0].innerHTML;
  if (backup_content.indexOf('<div style="writing-mode:vertical-rl;') !== -1) {
    var ppq = document.querySelectorAll("div.prp-page-qualityheader");
    if (ppq.length > 0) {
      target_div = paragraphs[0];
      var cb = document.getElementById("optimize-cb");
      if (cb === null) {
        var toggle_title = '調整樣式:';
        if (document.getElementById("p-variants-label").innerHTML.indexOf('<span>简体</span>') !== -1) {
          toggle_title = '调整样式:';
        }
        ppq[0].innerHTML = ppq[0].innerHTML + '<span style="float:right">' + toggle_title + '<input id="optimize-cb" type="checkbox" disabled checked onclick="toggle_vertical_content(this)" /><span>';
        optimize_vertical_content(paragraphs[0], backup_content);

        setTimeout(post_optimization, 500);
      }
    }
  }
}

function post_optimization() {
  var cb = document.getElementById("optimize-cb");
  if (cb === null) {
    setTimeout(post_optimization, 500);
    return;
  }

  if (optimized_done === false) {
    // adjust_small_skchar
    var skchars = document.querySelectorAll("div.sfrac > span > span > img");
    if (skchars.length > 0) {
      var i;
      for (i = 0; i < skchars.length; i++) {
        skchars[i].width = "16";
        skchars[i].height = "16";
      }
    }

    // punctuations
    var texts = document.querySelectorAll("div.mw-parser-output > div > div");
    var k;
    for (k = 0; k < texts.length; k++) {
      var t = texts[k].innerHTML;
      var ts = t.split(/<|>/);
      var t2 = "";
      var j;
      for (j = 0; j < ts.length; j++) {
        var s = ts[j];
        if (s.startsWith('img') || s.startsWith('div') || s.startsWith('span') || s.startsWith('p') || s.startsWith('a href')
          || s.startsWith('table ') || s === 'br' || s === 'pre' || s.startsWith('th') || s.startsWith('tr') || s.startsWith('td')) {
          // element start
          t2 = t2 + "<" + ts[j] + ">";
        } else if (ts[j].startsWith('/')) {
          // element end
          t2 = t2 + "<" + ts[j] + ">";
        } else {
          // punctuation: 「 」 “ ”
          t2 = t2 + ts[j].replace(/「/g, '<span style="display: inline-block; margin-left: -15px;">「</span>').replace(/」/g, '<span style="margin-left:-9px">」</span>').replace(/“/g, '<span style="display: inline-block; margin-left: ' + opening_quote_margin_left + '">“</span>').replace(/”/g, '<span style="margin-left:-9px">”</span>');
        }
      }
      texts[k].innerHTML = t2;
    }

    optimized_content = document.querySelectorAll("div#mw-content-text div.mw-parser-output")[0].innerHTML;
    optimized_done = true;
  }
  cb.disabled = false;
}

function optimize_vertical_content(element, content) {
  if (optimized_content === "") {
    optimized_content = content.replace(/vertical-rl/g, 'initial').replace(/<p><br>/g, '<div style="width:1.6em; line-height:1.2em; float:right; margin:auto 0.5em; border-right: gray dotted 1px">').replace(/<p>/g, '<div style="width:1.6em; line-height:1.2em; float:right; margin:auto 0.5em; border-right: gray dotted 1px">');

    // CJK space
    optimized_content = optimized_content.replace(/  ※此字在您的系/g, '  ※此字在您的系').replace(/ /g, '<div> </div>');

    // double-line comments and SKchar
    optimized_content = optimized_content.replace(/<\/p>/g, '</div>').replace(/<span style="display:inline-block; vertical-align:middle;color:#996666">/g, '<span style="display:inline-block; vertical-align:middle;color:#996666;float:right;width:3em;line-height:1.412em">').replace(/<span style="display:block">/g, '<span style="display:block;float:right;width:1.5em">').replace(/SKQSfont\.pdf\.jpg 2x" data-file-width="118" data-file-height="118"/g, 'SKQSfont.pdf.jpg 2x" data-file-width="118" data-file-height="118" style="margin-left:-2px"').replace(/<span style="color:transparent;font-size:0px">〈<\/span>/g, '').replace(/<span style="color:transparent;font-size:0px">〉<\/span>/g, '').replace(/<small class="sfrac nowrap;">/g, '<div class="sfrac nowrap;" style="font-size:0.85em">').replace(/<\/small>/g, '</div>');

    // punctuations after skchar (Sfari, Chrome)
    optimized_content = optimized_content.replace(/style="margin-left:-2px">,/g, 'style="margin-left:-2px"><div style="display: inline-block; width: 6px">,</div>').replace(/style="margin-left:-2px">;/g, 'style="margin-left:-2px"><div style="display: inline-block; width: 6px">;</div>').replace(/style="margin-left:-2px">。/g, 'style="margin-left:-2px"><div style="display: inline-block; width: 6px">。</div>').replace(/style="margin-left:-2px">?/g, 'style="margin-left:-2px"><div style="display: inline-block; width: 6px">?</div>').replace(/style="margin-left:-2px">:/g, 'style="margin-left:-2px"><div style="display: inline-block; width: 6px">:</div>').replace(/style="margin-left:-2px">、/g, 'style="margin-left:-2px"><div style="display: inline-block; width: 6px">、</div>').replace(/style="margin-left:-2px">!/g, 'style="margin-left:-2px"><div style="display: inline-block; width: 6px">!</div>');

    // punctuations after skchar (Firefox)
    optimized_content = optimized_content.replace(/style="margin-left:-2px" width="20" height="20">,/g, 'style="margin-left:-2px" width="20" height="20"><div style="display: inline-block; width: 6px">,</div>').replace(/style="margin-left:-2px" width="20" height="20">;/g, 'style="margin-left:-2px" width="20" height="20"><div style="display: inline-block; width: 6px">;</div>').replace(/style="margin-left:-2px" width="20" height="20">。/g, 'style="margin-left:-2px" width="20" height="20"><div style="display: inline-block; width: 6px">。</div>').replace(/style="margin-left:-2px" width="20" height="20">?/g, 'style="margin-left:-2px" width="20" height="20"><div style="display: inline-block; width: 6px">?</div>').replace(/style="margin-left:-2px" width="20" height="20">:/g, 'style="margin-left:-2px" width="20" height="20"><div style="display: inline-block; width: 6px">:</div>').replace(/style="margin-left:-2px" width="20" height="20">、/g, 'style="margin-left:-2px" width="20" height="20"><div style="display: inline-block; width: 6px">、</div>').replace(/style="margin-left:-2px" width="20" height="20">!/g, 'style="margin-left:-2px" width="20" height="20"><div style="display: inline-block; width: 6px">!</div>');
  }
  element.innerHTML = optimized_content;
}

function resume_vertical_content(element, content) {
  element.innerHTML = content;
}

function toggle_vertical_content(element) {
  if (element.checked === false) {
    resume_vertical_content(target_div, backup_content);
  } else {
    optimize_vertical_content(target_div, backup_content);
  }
}