Add Comic Sans MS to Google Docs/Slides (More Fonts + Search)

Adds Comic Sans MS to Google Docs/Slides, visible in "More Fonts" and when searched

目前為 2024-09-12 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Add Comic Sans MS to Google Docs/Slides (More Fonts + Search)
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Adds Comic Sans MS to Google Docs/Slides, visible in "More Fonts" and when searched
// @author       YourName
// @match        https://docs.google.com/document/d/*
// @match        https://docs.google.com/presentation/d/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to inject Comic Sans MS into "More Fonts" menu
    function injectComicSansInMoreFonts() {
        let observer = new MutationObserver(function(mutations) {
            mutations.forEach(function(mutation) {
                // Check if "More Fonts" menu exists
                let moreFontsDialog = document.querySelector('.docs-fontmenu ul');

                if (moreFontsDialog && !document.querySelector('.comic-sans-more-font-option')) {
                    // Create a new font option for Comic Sans MS
                    let newFontOption = document.createElement('div');
                    newFontOption.classList.add('goog-menuitem', 'goog-menuitem-content', 'comic-sans-more-font-option');
                    newFontOption.setAttribute('role', 'menuitem');
                    newFontOption.setAttribute('data-font', 'Comic Sans MS');
                    newFontOption.style.fontFamily = '"Comic Sans MS", cursive, sans-serif';
                    newFontOption.textContent = 'Comic Sans MS';

                    // Append the Comic Sans MS font to the "More Fonts" menu
                    moreFontsDialog.appendChild(newFontOption);

                    // Event listener to apply the font when selected from "More Fonts"
                    newFontOption.addEventListener('click', function() {
                        document.execCommand('fontName', false, '"Comic Sans MS"');
                    });
                }
            });
        });

        observer.observe(document.body, { childList: true, subtree: true });
    }

    // Function to detect typing in the font search box and load Comic Sans MS
    function addComicSansOnSearch() {
        let searchObserver = new MutationObserver(function(mutations) {
            mutations.forEach(function(mutation) {
                let fontSearchBox = document.querySelector('[aria-label="Font"]');

                if (fontSearchBox && !document.querySelector('.comic-sans-searched-option')) {
                    // Add event listener to detect when the user types into the font search box
                    fontSearchBox.addEventListener('input', function(e) {
                        if (e.target.value.toLowerCase() === 'comic sans ms') {
                            document.execCommand('fontName', false, '"Comic Sans MS"');
                        }
                    });
                }
            });
        });

        searchObserver.observe(document.body, { childList: true, subtree: true });
    }

    // Run the function to add Comic Sans MS after a delay to ensure the page has loaded
    window.setTimeout(injectComicSansInMoreFonts, 2000);
    window.setTimeout(addComicSansOnSearch, 2000);

})();