简体   繁体   English

电子:防止<style> tags in DOM from loading

[英]Electron: Prevent <style> tags in DOM from loading

Is there a way to remove all styling from an Electron webview before the page is shown? 有没有办法在显示页面之前从Electron Webview删除所有样式?

So far, I can inject JS to remove all styling after the document is loaded: 到目前为止,我可以注入JS以在文档加载后删除所有样式:

document.addEventListener("DOMContentLoaded", function(){
  document.querySelectorAll('link[rel="stylesheet"], style').forEach(elem => elem.disabled = true);
});

The problem here is that the styles will show for a brief moment (~1 second) before styling is removed. 这里的问题是样式将在删除样式之前显示一小段时间(约1秒)。

I can also use the following in my Main class to remove all .css files prior to page load: 我还可以在Main类中使用以下命令在页面加载之前删除所有.css文件:

session.webRequest.onBeforeRequest({urls: ['https://*/*.css', 'http://*/*.css']}, function(details, callback) {
  callback({cancel: true});
});

This, however, does not remove tags embedded in the DOM. 但是,这不会删除嵌入在DOM中的标签。 For a better idea of what I'm trying to do, you can check out Firefox's "No Style" feature (View -> Page Style -> No Style). 要更好地了解我要做什么,可以查看Firefox的“无样式”功能(查看->页面样式->无样式)。

Thanks in advance. 提前致谢。

There are two ways I can think of to prevent this: 我有两种方法可以防止这种情况发生:

  1. Give the webview a visibility:hidden style, and make it visible later. 使Web视图具有可见性:隐藏的样式,并在以后使其可见。
  2. Point to a forwarding proxy who will strip out inline as well as linked CSS 指向将去除内联和链接CSS的转发代理

Number 1 sounds to me a quicker and more reliable solution, but I haven't tried it out. 第一个听起来对我来说是一种更快,更可靠的解决方案,但是我还没有尝试过。

Let me know how it goes. 让我知道事情的后续。

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

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