通过 WKWebView 应用于 Javascript 的样式表在模拟器上有效,在设备上失败
Stylesheet applied to Javascript via WKWebView works on simulator, fails on device
通过以下设置,我可以在模拟器上 运行 我的应用程序并且样式表已正确应用于内容。但是,运行在设备上使用完全相同的代码会导致样式表被忽略。
我如何设置 WKWebView
:
let contentController = WKUserContentController()
let userScript = WKUserScript(source: "my script goes here",
injectionTime: .atDocumentStart,
forMainFrameOnly: false)
contentController.addUserScript(userScript)
let configuration = WKWebViewConfiguration()
configuration.userContentController = contentController
webView = WKWebView(frame: .zero, configuration: configuration)
// Finish setting up & running web view here
对于上面的部分,将injectionTime
更改为.atDocumentEnd
并将forMainFrameOnly
更改为true
没有任何效果。
还有一个简化的 index.html 文件,它非常空:
<html>
<head>
<title>Style This Page</title>
</head>
<link rel="stylesheet" type="text/css" href="style.css" />
<body onload="runScript()">
<div id="scriptAttachesHere"></div>
<script>
let script
window.onload = function() {
let options = { }
script = new window.SCRIPT.Name(document.getElementById('scriptAttachesHere'), options);
}
</script>
</body>
</html>
以及样式表中的一段代码:
.script-container .devices .contentContainer::before { height: unset !important; }
所有名字都被屏蔽了,所以有可能是我做错了。
事实证明这很简单。
Apple 不喜欢将本地文件加载到 WKWebView
,因此我通过将我的样式 sheet 直接合并到我的 HTML 代码中解决了这个问题,而不是独立的样式 sheet.
<html>
<head>
<title>Style This Page</title>
</head>
<style>
.script-container .devices .contentContainer::before { height: unset !important; }
</style>
<body onload="runScript()">
<div id="scriptAttachesHere"></div>
<script>
let script
window.onload = function() {
let options = { }
script = new window.SCRIPT.Name(document.getElementById('scriptAttachesHere'), options);
}
</script>
</body>
</html>
请注意删除链接到本地样式的代码sheet。
通过以下设置,我可以在模拟器上 运行 我的应用程序并且样式表已正确应用于内容。但是,运行在设备上使用完全相同的代码会导致样式表被忽略。
我如何设置 WKWebView
:
let contentController = WKUserContentController()
let userScript = WKUserScript(source: "my script goes here",
injectionTime: .atDocumentStart,
forMainFrameOnly: false)
contentController.addUserScript(userScript)
let configuration = WKWebViewConfiguration()
configuration.userContentController = contentController
webView = WKWebView(frame: .zero, configuration: configuration)
// Finish setting up & running web view here
对于上面的部分,将injectionTime
更改为.atDocumentEnd
并将forMainFrameOnly
更改为true
没有任何效果。
还有一个简化的 index.html 文件,它非常空:
<html>
<head>
<title>Style This Page</title>
</head>
<link rel="stylesheet" type="text/css" href="style.css" />
<body onload="runScript()">
<div id="scriptAttachesHere"></div>
<script>
let script
window.onload = function() {
let options = { }
script = new window.SCRIPT.Name(document.getElementById('scriptAttachesHere'), options);
}
</script>
</body>
</html>
以及样式表中的一段代码:
.script-container .devices .contentContainer::before { height: unset !important; }
所有名字都被屏蔽了,所以有可能是我做错了。
事实证明这很简单。
Apple 不喜欢将本地文件加载到 WKWebView
,因此我通过将我的样式 sheet 直接合并到我的 HTML 代码中解决了这个问题,而不是独立的样式 sheet.
<html>
<head>
<title>Style This Page</title>
</head>
<style>
.script-container .devices .contentContainer::before { height: unset !important; }
</style>
<body onload="runScript()">
<div id="scriptAttachesHere"></div>
<script>
let script
window.onload = function() {
let options = { }
script = new window.SCRIPT.Name(document.getElementById('scriptAttachesHere'), options);
}
</script>
</body>
</html>
请注意删除链接到本地样式的代码sheet。