Add Background to Body

Adds a background to websites that have transparency in your browser (e.g. Zen Browser).

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Add Background to Body
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  Adds a background to websites that have transparency in your browser (e.g. Zen Browser).
// @author       Maniac
// @match        *://*/*
// @grant        none
// @license      MIT
// ==/UserScript==


(function() {
    'use strict';

/*
This generally should deal with most websites that have the transparency issue.
*/
    // Apply background color before the page starts rendering
    document.documentElement.style.backgroundColor = "#ffffff"; // White background

    window.addEventListener('DOMContentLoaded', function() {
        const bodyStyle = window.getComputedStyle(document.body);

        if (!bodyStyle.backgroundImage && bodyStyle.backgroundImage === 'none' && bodyStyle.backgroundColor === 'rgba(0, 0, 0, 0)' && !bodyStyle.background) {
            document.body.style.backgroundColor = "#ffffff"; // Ensure body background stays white
        }
    });

/*
This was a fix for Brave Search that could help with other websites.
This takes the background color of the body and makes a div with that background color in the very back.
*/
    const bgDiv = document.createElement("div");
    bgDiv.style.position = "fixed";
    bgDiv.style.top = "0";
    bgDiv.style.left = "0";
    bgDiv.style.width = "100vw";
    bgDiv.style.height = "100vh";
    bgDiv.style.zIndex = "-1"; // Send it to the back
    bgDiv.style.background = getComputedStyle(document.body).backgroundColor || "white";

    // Insert at the very beginning of the body
    document.body.prepend(bgDiv);
})();