Fix underscores in Outlook

Stops underscores from being converted into italics in the Outlook message editor.

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Fix underscores in Outlook
// @namespace    http://tampermonkey.net/
// @author       yotann
// @version      0.1
// @license      CC-PDDC
// @description  Stops underscores from being converted into italics in the Outlook message editor.
// @match        https://outlook.office.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function hookPrototype(prototype, overrides) {
        // Set the prototype of the overrides, so `super` refers to an unmodified copy of the original prototype.
        Object.setPrototypeOf(overrides, Object.create(Object.getPrototypeOf(prototype), Object.getOwnPropertyDescriptors(prototype)));
        Object.defineProperties(prototype, Object.getOwnPropertyDescriptors(overrides));
    }

    // Javascript modules are lazily loaded using webpack chunks.
    // Detect when a new chunk is loaded, so we can find the module that includes MarkdownPlugin.
    hookPrototype(globalThis.webpackChunkOwa, {
        push(array) {
            const moduleConstructors = array[1];
            Object.entries(moduleConstructors).forEach(([id, originalModuleConstructor]) => {
                moduleConstructors[id] = function(module, exports, exporter) {
                    originalModuleConstructor.apply(this, arguments);
                    if (exports.hasOwnProperty('MarkdownPlugin')) {
                        console.log('Patching MarkdownPlugin');
                        hookPrototype(exports.MarkdownPlugin.prototype, {
                            initialize(editor) {
                                super.initialize(editor);
                                this.options.italic = false; // "_"
                                // this.options.bold = false; // "*"
                                // this.options.strikethrough = false; // "~"
                                // this.options.codeFormat = false; // "`"
                            }
                        });
                    }
                };
            });
            super.push(array);
        }
    });
})();