简体   繁体   English

是否可以设置 webpack 开发服务器客户端覆盖的样式?

[英]Is it possible to style the webpack dev server client overlay?

I'm using an Angular application, and when I make a mistake in the code the webpack overlay div shows up in my browser.我正在使用 Angular 应用程序,当我在代码中出错时,webpack 覆盖 div 会显示在我的浏览器中。 The iframe for this can be styled using the dev tools in the web browser by adding a style on the div like so: iframe 可以使用 web 浏览器中的开发工具设置样式,方法是在 div 上添加样式,如下所示:

#webpack-dev-server-client-overlay-div {
  font-size: 11px !important;
}

Is there a way to write a style file for this iframe?有没有办法为此 iframe 编写样式文件? I've tried adding styles to my Angular application's styles.scss file, but it has no effect (I guess because it's in an iframe subelement inside my application). I've tried adding styles to my Angular application's styles.scss file, but it has no effect (I guess because it's in an iframe subelement inside my application). The goal is to make the text smaller, and maybe have more control of how the text is shown -- for some unfortunate reason, the default text size is "large".目标是使文本更小,并且可以更好地控制文本的显示方式——由于某些不幸的原因,默认文本大小为“大”。

Quoted from another stackoverflow answer on a similar topic:引用自另一个关于类似主题的 stackoverflow 答案:

If you are trying to target the markup which is loaded inside the iframe than that's not possible.如果您试图定位在 iframe 中加载的标记,那是不可能的。

The above is not applicable in this case, because webpack is open source and it should be easy to configure the iframe (either monkey patching, using an existing configuration value, or writing a webpack plugin)以上不适用于这种情况,因为 webpack 是开源的,并且应该很容易配置 iframe(猴子补丁,使用现有配置值,或编写 Z424516CA53B4AD4BEF37ED04F8795A88 插件)

You can not change webpack error template directly with app style but you can overwrite any style with the stylus extension (as a browser extension).您不能直接使用应用程序样式更改 webpack 错误模板,但您可以使用手写笔扩展(作为浏览器扩展)覆盖任何样式。 Just install and write your own style.只需安装并编写自己的风格。

Edit: I didn't try the above method before and tried now, results: fail.编辑:我之前没有尝试过上述方法,现在尝试,结果:失败。 The above method is not working because of webpack using iframe.上述方法不起作用,因为 webpack 使用 iframe。 We can't overwrite the iframe's CSS.我们无法覆盖 iframe 的 CSS。 So, we need to use the javascript option and at the same time, we should write each refresh.因此,我们需要使用 javascript 选项,同时,我们应该编写每次刷新。 Now, I am offering the TamperMoney extension.现在,我提供TamperMoney扩展。 This extension allows the running of custom JS scripts on the target webpage.此扩展允许在目标网页上运行自定义 JS 脚本。 Install and create a new user script.安装并创建一个新的用户脚本。

Here is my scenario;这是我的场景; I want to force the light mode when the page reloads.我想在页面重新加载时强制使用灯光模式。

// ==UserScript==
// @name         Webpack Error Screen Light Mode
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       EgoistDeveloper
// @match        http://localhost:4200/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=undefined.localhost
// @grant        none
// ==/UserScript==

(function () {
    "use strict";

    // overwrite with custom CSS
    function forceToLightMode() {
        // if browser is not in dark mode, force it to be
        if (
            window.matchMedia &&
            !window.matchMedia("(prefers-color-scheme: dark)").matches
        ) {
            const styleElement = document.createElement("style");
            styleElement.textContent = `#webpack-dev-server-client-overlay-div {
                background-color: rgb(243 243 243 / 85%) !important;
                color: rgb(0 0 0) !important;
            }`;

            const iframeElement = document.getElementById("webpack-dev-server-client-overlay"),
                iframeOverlay = iframeElement.contentWindow.document.getElementsByTagName("head");

            iframeOverlay[0].appendChild(styleElement);
        }
    }

    setTimeout(() => {
        forceToLightMode();
    }, 250);

    setInterval(() => {
        forceToLightMode();
    }, 3000);
})();

Note: you should change user script meta configs with yours and done.注意:您应该使用您的更改用户脚本元配置并完成。 Actually, both of these methods are valid in many cases.实际上,这两种方法在很多情况下都是有效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM