gpt4 book ai didi

javascript - 使用 cocoa 将 JavaScript 注入(inject)网站时遇到问题

转载 作者:行者123 更新时间:2023-12-03 17:56:41 24 4
gpt4 key购买 nike

我正在尝试将一些 JavaScript 注入(inject)到不属于我的网站中。我尝试注入(inject)的代码如下:

function submitFormForTime(time){
$(".t_h").each(function(i, obj){ // find each element with the class t_h
if ($(this).text() != time) return; // not the right one
$(this).parent().find("form").each(function(i, obj){
obj.submit(); // submit that form
});
});
}

这不起作用。看来我正在使用的方法 stringByEvaluatingJavaScriptFromString 不适用于嵌套括号或其他内容。使用简单的代码就可以很好地工作。

问题是代码根本没有被注入(inject)

我的完整代码如下:

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function submitFormForTime(time){ "
"$(\".t_h\").each(function(i, obj){"
"if ($(this).text() != time) return;"
"$(this).parent().find(\"form\").each(function(i, obj){"
"obj.submit();"
"});"
"});"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);"]];

任何帮助将不胜感激。谢谢

最佳答案

好吧,没有太多麻烦,我就可以看到一些小东西。我重写了,也许你可以尝试一下。

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@
"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = function submitFormForTime(time) { $('.t_h').each(function(i, obj) { if ($(this).text() == time) $(this).parent().find('form').each(function(i, obj){ obj.submit(); }); }); } document.getElementsByTagName('head')[0].appendChild(script);"]];

我希望它能像这样工作。

基本上我删除了一些双引号,并用单引号替换了一些,以便不必转义它们。我将所有脚本文本写在一行上。

如果 form 是一个类,则 find('form') 应该是 find('.form');如果 form 是一个 id,则 find('#form') 应该是 find('#form')。

关于javascript - 使用 cocoa 将 JavaScript 注入(inject)网站时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12227297/

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