JavaFx WebEngine - 用(本地)文件覆盖网站的样式表 [英] JavaFx WebEngine - Overwriting a website's stylesheet with (local) files
问题描述
我想自定义一个网站的外观,我加载,所以我创建了一个小的 test.css
文件,除了改变所有的外观表格行:
I'd like to customise the appearance of a website that I am loading, so I created a little test.css
file that does nothing but changing the look of all table rows:
tr {
height: 22px;
background-image: url("test.png");
}
如何让WebEngine加载此文件并替换页面自己的CSS规则与我的
此外,我想能够加载页面特定的CSS文件,而不是一个巨大的文件的所有页面。
How do I get he WebEngine to load this file and replace the page's own CSS rules with mine? Also, i'd like to be able to load page-specific css files and not one huge file for all pages.
我发现此页面,但它只显示如何通过DOM运行,并手动分配一个新的风格到所需的元素。这当然不是我想要的。
I found this page, but it only shows how to run through the DOM and assign a new style to the desired elements by hand. This is, of course, not what I want. Instead, I'd like the browser to use my files as 'user defaults'.
Thx为任何帮助:)
Thx for any help :)
推荐答案
首先我要说明,我希望你知道你在做什么,因为这些东西会严重损害一个网站。
所以这里是你可以做的:
First of I have to state, that I hope you know what you are doing, as these things can seriously damage a web site. So here is what you can do:
你从<$ c $>获取文档
c> WebEngine ,撤销头
元素并向其中添加 style
子元素,包含您要添加的样式表的src位置。
You grab the Document
from the WebEngine
, retreave the head
element and add a style
child element to it, containing the src location of the stylesheet you want to add.
Document doc = webView.getEngine().getDocument();
URL scriptUrl = getClass().getResource(pathToAttachedDocument); // pathToAttachedDocument = CSS file you want to attach
String content = IOUtils.toString(scriptUrl); // Use Apache IO commons for conveniance
Element appendContent = doc.createElement("style");
appendContent.appendChild(doc.createTextNode(content));
doc.getElementsByTagName("head").item(0).appendChild(appendContent);
顺便说一下,JavaScript可以以类似的方式添加,它只是'script' style'
By the way, JavaScript can be added in a similar way, it's just 'script' instead of 'style'
这篇关于JavaFx WebEngine - 用(本地)文件覆盖网站的样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!