AO3 Google Docs Italics Bug Fixer

Adds button to AOOO's editor. Click on it in order to auto-fix the Google Docs import space bug.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         AO3 Google Docs Italics Bug Fixer
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Adds button to AOOO's editor. Click on it in order to auto-fix the Google Docs import space bug.
// @author       exuvia
// @match        https://archiveofourown.org/works/*
// @icon         http://archiveofourown.org/favicon.ico
// @grant        none
// ==/UserScript==
//An explanation of the bug: https://archiveofourown.org/works/6981169/chapters/15908851
//No more needing to italicize the punctuation to get rid of the bug!
//Paste your work from Google Docs into the Rich Text editor, then click the Fix Italics (Gdocs) button to remove the improper formatting that adds the unwanted space.
//Will not remove spaces if you paste in, click Preview, go back to the edit page, click Rich Text, then click HTML. In this convoluted case just click Preview and go back to the edit page again and the Fix Italics button will work. Or refresh.

(function() {

	if (document.querySelectorAll(".rtf-html-switch.actions").length > 0){ //if is on an Edit Work page with the relevant buttons
		let fixbtn = document.createElement("a");
		fixbtn.innerHTML = "Fix Italics (Gdocs)";
		fixbtn.onclick = ()=>{
			let fixItalics = () => {
				//removes span elements that are around <em> or </em> tags
				document.getElementById("content").value = document.getElementById("content").value.replaceAll(/<\/*span[^>]*>\s*(?=<\/*em>)|(?<=<\/*em>)\s*<\/*span[^>]*>/g,"");
			}
			if (document.getElementById("content").style.display === "none") {
				document.querySelectorAll(".html-link")[0].click();
				window.setTimeout(()=>fixItalics(),200);
			}
			else fixItalics();
		}
		document.querySelectorAll(".rtf-html-switch.actions")[0].appendChild(fixbtn);
	}

})();