问题标签 [wkuserscript]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
8149 浏览

javascript - WKUserScript 不工作

我想使用WKWebviewAPI 注入脚本。由于某种原因它不起作用,我无法弄清楚。我已经尝试在Safari开发人员控制台中进行调试,但我也无法JavaScript在其中找到代码。

实现代码如下:

0 投票
1 回答
644 浏览

javascript - WKUserScript 在 iOS10 中不调用,但在 iOS9 中有效

我的代码在 iOS 9 中完美运行,但在 iOS 10 中无法运行,特别是 doBar() 未调用。在 WKWebView 中,我正在注入 javascript 代码。

在网页 js 代码中进行调用:

Safari 调试器显示在 iOS 10 中 window.MyInterface 未定义,但存在带有 doBar 代码的用户脚本。

假设我无法更改 Web 代码,如何正确注入 doBar,以便它可以在 iOS 10 中运行?

0 投票
1 回答
587 浏览

ios - WKUserScript for MainFrameOnly

WKUserScript 有这个init

init(source: String, injectionTime: WKUserScriptInjectionTime, forMainFrameOnly: Bool)

'MainFrame' 是什么意思?

0 投票
2 回答
531 浏览

javascript - 每隔几秒刷新一次外部网页

为了工作,我在特定领域的论坛上花费了大量时间。一整天,我都会查看这些论坛,看看是否有任何与我的工作相关的问题和答案。但是,我使用的特定站点不会自动更新,因此我必须刷新它。这没什么大不了的,但我决定将以下 JavaScript 脚本保存为书签:

本质上,我想强制页面每秒重新加载。但是,当它重新加载时,它似乎会导致我的脚本停止运行,因此我的脚本的唯一效果是一次重新加载。这并不比手动刷新网站更好。

我想更普遍的问题是“有没有办法让脚本即使在用户访问新站点时也能保持运行?” 我猜不是,因为当您浏览互联网的其余部分时,网站可能会跟踪您。尽管如此,似乎可能存在脚本执行级别。也许从书签执行脚本与网页上的 <script> 标记不同;如果我是浏览器设计师,我会考虑到这一点,并将其视为独立于当前加载页面的幕后运行的“浏览器代码”。这是浏览器的工作方式,还是它认为所有脚本都只是在当前加载的页面上运行?

谢谢!

0 投票
1 回答
479 浏览

javascript - 从 WKUserContentController 中删除单个特定用户脚本

我正在使用 addUserScript() 将用户脚本添加到我的 WKWebView 的 WKUserContentController。我注意到即使调用 loadRequest() 脚本仍然存在。

对于某些用例,我可能需要删除某些脚本并添加其他脚本。但是,删除脚本的唯一方法似乎是 removeAllUserScripts()。这非常不方便,因为这意味着我必须删除所有脚本,然后重新添加我想要保留的脚本。

如果有人知道删除特定脚本的任何方法(按名称、句柄等),请告诉我。

0 投票
0 回答
63 浏览

javascript - javascript注入未执行

我已经包含了我的函数以及我调用它的方式确实在控制台中打印“注入”,但似乎注入了 javascript dosent 我已经在 safari 控制台和 chrome 控制台中测试了 js,它可以工作,但在 wkwebview 中可以工作

0 投票
1 回答
206 浏览

ios14 - iOS 14 上的 WKUserScript init 崩溃:使用未实现的初始化程序 init(source:injectionTime:forMainFrameOnly:in:)

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

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

类 MyScript:WKUserScript {

}

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);"

0 投票
0 回答
38 浏览

javascript - 当应用程序进入后台 WKWebView 时如何评估 JavaScript?

我正在使用 HTML5 IMA SDK 播放器创建 html 页面,然后将此页面加载到 WKWebView,所有工作正常暂停、播放内容等,但我希望在应用程序进入后台时“暂停”内容以实现这一点我确实尝试过使用“WKWebView evaluateJavaScript”但是当应用程序进入后台并尝试过这个答案时,这段代码不起作用(https://stackoverflow.com/questions/53482566/wkwebview-doesnt-run-javascript-when-on-background#:~:text=Javascript %20is%20disabled%20by%20default,WKWebView%20should%20look%20like%20this.&text=Also%20you%20can%20check%20this%20post%20to%20see%20a%20different%20approach)但这对我没有帮助, keyWindow 已弃用,我使用另一种方法:

也不行。

当应用程序进入后台或在应用程序进入后台之前,“evaluateScript”存在哪种方式?