简体   繁体   English

Javascript 异步函数在 url 栏中 js 的 android chrome 中不起作用

[英]Javascript async functions doesn't work in android chrome from js in url bar

I made a simple script to run in my android chrome.我制作了一个简单的脚本来在我的 android chrome 中运行。 It comes that there are 2 scenarios working differently:有两种不同的工作方式:

first one javascript:(alert("try")) works with no problems第一个javascript:(alert("try"))没有问题

second one javascript:(async function test(){alert("try")};test()) does NOT work第二个javascript:(async function test(){alert("try")};test())不起作用

obv it's not what i am trying to do but in my desired script i need an await or a generic sleep. obv 这不是我想要做的,但在我想要的脚本中,我需要等待或通用睡眠。 What can i do?我能做些什么?

edit: the curious thing is that setInterval works fine... but javascript:(let prova = "try";alert(prova)) doesn't work编辑:奇怪的是 setInterval 工作正常......但是javascript:(let prova = "try";alert(prova))不起作用

edit2: i did it, it took a very bad headache but now works.编辑2:我做到了,头疼得很厉害,但现在可以了。 There are things that doesn't work for some reason but a workaround can always be found有些事情由于某种原因不起作用,但总能找到解决方法

here's what's the final working version这是最终的工作版本

javascript:(setInterval(() =>{setTimeout(() => {document.querySelector('.nameofthetextareaselector').value="value i wanted to put here";document.querySelector('.nameofthebuttontosend').click();new Promise(resolve => setTimeout(() => {document.querySelector('.nameofthesecondbutton').click()},2000));resolve},2000)},5000))

It edits the textarea, send it, click another button after some seconds and repeat它编辑文本区域,发送它,几秒钟后单击另一个按钮并重复

things that not worked: assign anything with let, const or directly the name of the variable, nesting promises, using async-await, using try-catch, etc...无效的事情:使用 let、const 或直接分配变量名、嵌套 promise、使用 async-await、使用 try-catch 等...

Now i am happy that all works but i want to know why all these things doesn't seem to work (only on android chrome, on windows chrome all these things worked)现在我很高兴一切正常,但我想知道为什么所有这些东西似乎都不起作用(仅在 android chrome 上,在 windows chrome 上所有这些东西都有效)

您现在所做的只是声明一个函数而不调用它,您可以在 javascript 函数的末尾添加()以立即执行它。

javascript:(async function test(){alert("try")}())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM