gpt4 book ai didi

javascript - UIWebView 不使用方法 stringByEvaluatingJavaScriptFromString : 评估 java 脚本

转载 作者:行者123 更新时间:2023-11-29 03:20:59 26 4
gpt4 key购买 nike

我在从 UIWebView 评估 youtube api javascript 命令时遇到问题

命令在 Safari 和 Firefox 中正确评估

WebView 使用选项初始化

allowsInlineMediaPlayback = YES;
mediaPlaybackRequiresUserAction = NO;

我正在加载的 Html 是:

<div id="player"></div>

<script>
var tag = document.createElement('script');
//playerVars: { 'autoplay': 0, 'modestbranding': 1, 'rel': 0, 'showinfo': 0, 'iv_load_policy': 3, 'controls': 1, 'playsinline':1 },
var configuration = {
playerVars: {
'playsinline':1,
'showinfo': 0,
'modestbranding': 1
},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
};

tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

function setup(key, value) {
configuration[key] = value;
}

function configurePlayer(key, value) {
configuration.playerVars[key] = value;
}
// area for the code injection
setup('videoId', 'cDhlx60nTCU');
setup('startingTime', 10);
setup('autoPlay' , true);
setup('width', 400);
setup('height', 200);
configurePlayer('controls', true);


var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', configuration);
}

function onPlayerReady(event) {
if (configuration['autoPlay']) {
if (configuration['startingTime']) {
player.playVideoAt(configuration['startingTime']);
} else {
event.target.playVideo();
}
}
}

function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING) {
// Do something...
}
}

function stopVideo() {
player.stopVideo();
return "OK";
}
</script>
Printing description of html:
<div id="player"></div>

<script>
var tag = document.createElement('script');
//playerVars: { 'autoplay': 0, 'modestbranding': 1, 'rel': 0, 'showinfo': 0, 'iv_load_policy': 3, 'controls': 1, 'playsinline':1 },
var configuration = {
playerVars: {
'playsinline':1,
'showinfo': 0,
'modestbranding': 1
},
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
};

tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

function setup(key, value) {
configuration[key] = value;
}

function configurePlayer(key, value) {
configuration.playerVars[key] = value;
}
// area for the code injection
setup('videoId', 'cDhlx60nTCU');
setup('startingTime', 0);
setup('autoPlay' , true);
setup('width', 320);
setup('height', 288);
configurePlayer('controls', true);


var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', configuration);
}

function onPlayerReady(event) {
if (configuration['autoPlay']) {
if (configuration['startingTime']) {
player.playVideoAt(configuration['startingTime']);
} else {
event.target.playVideo();
}
}
}

function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.PLAYING) {
// Do something...
}
}

function stopVideo() {
player.stopVideo();
return "OK";
}
</script>

我正在用这个加载 WebView 页面

-(void)viewDidAppear:(BOOL)animated
{
[self.webview loadHTMLString:html baseURL:[NSURL URLWithString:@"html"]];
}

在 webview 委托(delegate)调用 webViewDidFinishLoad 之后

我试过用这种语法在播放器上调用方法

-(void)pause
{
[self.webview stringByEvaluatingJavaScriptFromString:@"player.pauseVideo();"];
}

是否可能有任何标志干扰 java 脚本评估?

最佳答案

改变这一行:

[webView loadHTMLString:embedHTML baseURL:[NSURL URLWithString:@"html"]];

为此:

[webView loadHTMLString:embedHTML baseURL:[NSURL URLWithString:@"about:blank"]];

我注意到当您不使用有效的 URL 时 stringByEvaluatingJavaScriptFromString 将不起作用(您会发现当您使用 NULL 作为值(value)。我刚刚用两个版本测试了你的代码,发现带有 about:blank 的版本将正确评估 JavaScript,而带有 html 作为 NSURL 值的版本将不是。

关于javascript - UIWebView 不使用方法 stringByEvaluatingJavaScriptFromString : 评估 java 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21099565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com