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.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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);
	}

})();