在 Chrome 中“当场”执行 Javascript 代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5603836/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executing Javascript code "on the spot" in Chrome?
提问by Gadi A
I have a page in chrome which contains many textboxes and I wish to feed values automatically to them. I already have the list of name-value pairs, so if I could simple execute a series of Javascript commands on the form "document.getElementsByName(NAME)[0].value = VALUE;" I'll be done (I hope...)
我有一个 chrome 页面,其中包含许多文本框,我希望自动向它们提供值。我已经有了名称-值对列表,所以如果我可以简单地在表单“document.getElementsByName(NAME)[0].value = VALUE;”上执行一系列 Javascript 命令 我会完成(我希望...)
So the question is - can I run a JS code "on the spot", or do I have to use a content script somehow?
所以问题是 - 我可以“当场”运行 JS 代码,还是必须以某种方式使用内容脚本?
回答by KooiInc
Right click on the page and choose 'inspect element'. In the screen that opens now (the developer tools), clicking the second icon from the left @ the bottom of it opens a console, where you can type javascript. The console is linked to the current page.
右键单击页面并选择“检查元素”。在现在打开的屏幕(开发人员工具)中,单击左侧@底部的第二个图标会打开一个控制台,您可以在其中键入 javascript。控制台链接到当前页面。
回答by JustinStolle
I'm not sure how far it will get you, but you can execute JavaScript one line at a time from the Developer Tool Console.
我不确定它会让您走多远,但您可以从Developer Tool Console一次执行一行 JavaScript 。
回答by klimat
You can use bookmarkletsif you want run bigger scripts in more convenient way and run them automatically by one click.
如果您想以更方便的方式运行更大的脚本并通过一键自动运行它们,您可以使用书签。
回答by Mathew
If you mean you want to execute the function inputted, yes, that is simple:
如果您的意思是要执行输入的函数,是的,这很简单:
Use this JS code:
使用这个JS代码:
eval(document.getElementById( -- el ID -- ).value);
回答by ShotgunToothpaste
Have you tried something like this? Put it in the head for it to work properly.
你有没有尝试过这样的事情?把它放在头上,让它正常工作。
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(){
//using DOMContentLoaded is good as it relies on the DOM being ready for
//manipulation, rather than the windows being fully loaded. Just like
//how jQuery's $(document).ready() does it.
//loop through your inputs and set their values here
}, false);
</script>