首页 > 解决方案 > iOS 14 上的 WKUserScript init 崩溃:使用未实现的初始化程序 init(source:injectionTime:forMainFrameOnly:in:)

问题描述

在 ios 14 中执行此 init 方法时崩溃

使用未实现的初始化程序 init(source:injectionTime:forMainFrameOnly:in:)

类 MyScript:WKUserScript {

init(source: String) {
    super.init(source: source,
               injectionTime: .atDocumentEnd,
               forMainFrameOnly: true)
}

}

let source = "var style = document.createElement('style');\nstyle.type = 'text/css'\nstyle.innerHTML = '/** * Style.css * FolioReaderKit * * 由 Heberti Almeida 创建于 06/ 05/15. * 版权所有 (c) 2015 Folio Reader。保留所有权利。// CSS 重置/html、body、div、span、applet、object、iframe、h1、h2、h3、h4、h5、h6、p、块引用,pre,a,abbr,首字母缩略词,地址,大,引用,代码,del,dfn,em,img,ins,kbd,q,s,samp,小,罢工,强,sub,sup,tt,var, b, u, i, center,dl, dt, dd, li,fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td,article, 一边, canvas, details, embed,图、图标题、页脚、页眉、hgroup、菜单、导航、输出、ruby、部分、摘要、时间、标记、音频、视频 { 边距:0;垂直对齐:基线;}/旧版浏览器的 HTML5 显示角色重置/article,aside, details, figcaption, figure,footer, header, hgroup, menu, nav, section { display: block;}/ ePUB /html { -webkit-text-size-adjust:没有任何; /从不自动调整文本大小/填充:0 0 !important; 溢出:隐藏;}body {填充:40px 20px!重要;溢出:隐藏!重要;} {分页前:初始!重要;/* 将分页符重置为默认值/}/平板电脑的自定义填充/@media only screen and (min-device-width: 768px){ body { padding: 60px 80px !important; }}//table { 边框折叠:折叠;边框间距:0;word-break: normal;}tbody, tfoot, thead { vertical-align: middle !important;}td, th, tr { vertical-align: inherit !important;}/ List /dd, dir, menu, ol, ul { margin-left: 30px !important; }ol { 列表样式类型:十进制!重要;}li { 显示:列表项!重要;}ol ol, ol ul, ul ol, ul ul { margin-bottom: 0 !important; margin-top: 0 !important;}/链接/a { -webkit-touch-callout: none; } /禁用链接回调/ { -webkit-user-select: text; }img { -webkit-user-select:无;}p { 边距:1.3em 0 1.5em 0; 行高:1.40em!重要;text-indent: 1.25em;}b, strong, th {font-weight: bolder !important;}/* 强制字体覆盖/code, kbd, pre, samp, tt { font-family: monospace, monospace !important; font-size: 1em;}button, input, select, textarea { display: inline-block !important; }/ h1, h2, h3, h4, h5, h6 { font-weight: 400!important; } /del,s,strike { 文本装饰:line-through!important;}hr { 背景颜色:rgba(0,0,0,.1) !important; 边框:无!重要;height: 1px !important;}/ Sub 和 Super /big { font-size: 1.15em !important; }small, sub, sup { font-size: .65em !important; }sub { 垂直对齐:sub !important; }sup { 字体系列:等宽!重要;vertical-align: super !important;}/ iBooks like/a { 文字装饰:无;}pre { 空白:预包装;}@page { 边距:0 0 !important; }table, ol, il { 文本对齐:-webkit-auto; }h1 ,h2 ,h3 ,h4 ,h5 ,h6 { 文本对齐:-webkit-auto; text-rendering: optimizelegibility;}/允许在标题和锚点上分词,因为它们往往更大的字体或包含更长的词/a, h1, h2, h3, h4, h5, h6 { word-break: break-word !重要的; -webkit-连字符:无!重要;连字符:无 !important;}/开始 Ted /img, svg, audio, video { max-height: 95% !important; 最大宽度:100%!重要;box-sizing:边框框;适合对象:包含;page-break-inside: Avoid;}/ End Ted // Divs 也用于调整图像大小,因此请确保作者得到他们想要的//这是为了让装在其中的图像在屏幕上完全可见/div { max-width: 100%; }aside[epub|type~="footnote"] { display: none !important; }ruby > rt, ruby​​ > rp { -webkit-user-select: none; } { -webkit-font-smoothing: subpixel-antialiased }/* * * 高亮类 * /highlight { -webkit-touch-callout: none; -webkit-user-select: none;}/移除点击高亮/input, textarea, button, highlight, select, a { -webkit-tap-highlight-color: rgba(0,0,0,0);}/高亮风格/html .highlight-yellow {background:rgb(255, 235, 107)}html .highlight-green {background:#C0ED72}html .highlight-blue {background:#ADD8FF}html .highlight-pink {background:#FFB0CA }html .highlight-underline { 文字装饰:无;边框底部:2px 实心#F02814;}html .highlight-yellow,html .highlight-green,html .highlight-blue,html .highlight-pink,span.epub-media-overlay-playing {border-radius:3px; 填充:0 2px;margin: 0 -2px;}/默认媒体覆盖样式/.mediaOverlayStyle0 span.epub-media-overlay-playing { background: #ccc}.mediaOverlayStyle1 .epub-media-overlay-playing { border-bottom: dotted 2px transparent; border-radius: 0;}/ * * 夜间模式 */html, body { -webkit-transition: all 0.6s ease;}html { background-color: #FFFFFF !important;}body { background-color: transparent !important;}html.nightMode, html.nightMode body { background-颜色:#131313 !important;}.nightMode p, .nightMode div, .nightMode span:not(.epub-media-overlay-playing) { color: #767676 !important; 背景颜色:透明!important;}.nightMode h1,.nightMode h2,.nightMode h3,.nightMode h4,.nightMode h5,.nightMode h6 { 颜色:#848484 !important;}html.nightMode .highlight-yellow {背景:rgba(255, 235, 107, 0.9)}html.nightMode .highlight-green {背景:rgba(192, 237, 114, 0.9)}html.nightMode .highlight-blue {背景:rgba(173, 216, 255) , 0.9)}html.nightMode .highlight-pink {background:rgba(255, 176, 202, 0.9)}html.nightMode .highlight-underline {border-bottom: 2px solid rgba(240, 40,* * 字体类 * */.andada, .andada p, .andada span, .andada div { font-family: "Andada", sans-serif !important; }.lato, .lato p, .lato span, .lato div { font-family: "Lato", serif !important; }.lora, .lora p, .lora span, .lora div { font-family: "Lora", serif !important; }.raleway, .raleway p, .raleway span, .raleway div { font-family: "Raleway", sans-serif !important; }html.textSizeOne, .textSizeOne body { 字体大小:13px !important; }html.textSizeTwo, .textSizeTwo body { 字体大小:15px !important; }html.textSizeThree, .textSizeThree body { font-size: 17px !important; }html.textSizeFour, .textSizeFour body { font-size: 19px !important; }html.textSizeFive, .textSizeFive body { font-size: 21px !important; }h1 { 字体大小:2em; 行高:1.2;}h2 { 字体大小:1.5em;行高:1.2;}h3 { 字体大小:1.17em; 行高:1.2;}h4 { 字体大小:1em;行高:1.2;}h5 { 字体大小:0.83em;行高:1.2;}h6 { 字体大小:0.67em;line-height: 1.2;}body { word-break: break-word !important; -webkit-连字符:自动!重要;连字符:auto !important;}p, span, div { font-size: 1em; line-height: 1.5 !important;}@media only screen and (min-device-width: 600px) { div { font-size: 1em; 行高:1.438em!重要;} body { -webkit-hyphens: none !important; 连字符:无!重要;}}';\ndocument.head.appendChild(style);" -webkit-连字符:自动!重要;连字符:auto !important;}p, span, div { font-size: 1em; line-height: 1.5 !important;}@media only screen and (min-device-width: 600px) { div { font-size: 1em; 行高:1.438em!重要;} body { -webkit-hyphens: none !important; 连字符:无!重要;}}';\ndocument.head.appendChild(style);" -webkit-连字符:自动!重要;连字符:auto !important;}p, span, div { font-size: 1em; line-height: 1.5 !important;}@media only screen and (min-device-width: 600px) { div { font-size: 1em; 行高:1.438em!重要;} body { -webkit-hyphens: none !important; 连字符:无!重要;}}';\ndocument.head.appendChild(style);"

标签: ios14wkuserscript

解决方案


iOS 14 及更高版本上 WKUserScript 初始化的问题。

例如:

  • 如果您有复杂的脚本逻辑来将您的 Web 内容连接到您的应用程序,但您的 Web 内容也有自己的复杂脚本库,您可以通过使用客户端 WKContentWorld 来避免可能的冲突。
  • 如果您正在编写一个支持 JavaScript 扩展的通用 Web 浏览器,您将为每个扩展使用不同的客户端 WKContentWorld。

由于 WKContentWorld 对象是一个命名空间,它本身不包含任何数据。例如:

  • 如果您在查看特定网页文档时将变量存储在特定 WKContentWorld 范围内的 JavaScript 中,则在导航到新文档后,该变量将消失。
  • 如果您在 JavaScript 中将变量存储在一个 WKWebView 中特定 WKContentWorld 的范围内,则该变量将不会存在于另一个 WKWebView 的同一世界中。

添加此行后的 Slove

类 MyScript:WKUserScript {

init(source: String) {
    if #available(iOS 14.0, *) {
        super.init(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true, in: .defaultClient)
    } else {
        // Fallback on earlier versions
        super.init(source: source,
                   injectionTime: .atDocumentEnd,
                   forMainFrameOnly: true)
    }
}

}


推荐阅读