javascript Chrome 扩展:打开选项卡,转到 url,填写表单并提交表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8183696/
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
Chrome Extension: Open tab, go to url, fill form and submit form
提问by giorgio79
I am following a tutorial here http://www.blackweb20.com/2010/01/11/creating-your-own-google-chrome-extension/
我在这里学习教程 http://www.blackweb20.com/2010/01/11/creating-your-own-google-chrome-extension/
I can open fine a tab with a custom extension and load a url, and I would like to fill and submit a form with javascript on the page opened. For example, could I submit a search on google.com?
我可以打开一个带有自定义扩展名的选项卡并加载一个 url,我想在打开的页面上填写并提交一个带有 javascript 的表单。例如,我可以在 google.com 上提交搜索吗?
Here is what I have so far:
这是我到目前为止所拥有的:
manifest.json
{
"name": "My First Extension",
"version": "1.0",
"description": "The first extension that I made.",
"browser_action": {
"default_icon": "icon.png"
},
"background_page": "background.html",
"permissions": [
"tabs"
]
}
background.html
背景.html
<script>
// get tab http://stackoverflow.com/questions/1979583/how-can-i-get-the-url-for-a-google-chrome-tab
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.create({'url': "http://google.com"}, function(tab) {
// Tab opened. Wait until page loads, from here it is not working
jQuery(document).ready(function() {
jQuery('#tsf').submit();
});
});
});
</script>
采纳答案by abraham
Your jQuery code is getting executed in the background page not the new tab. Try using chrome.tabs.executeScriptto execute the submit in the tab environment.
您的 jQuery 代码是在后台页面而不是新选项卡中执行的。尝试使用chrome.tabs.executeScript在 tab 环境中执行提交。
回答by Gokul N K
While you could do this using the chrome extension, I would suggest you to look into Selenium Browser Automation
虽然您可以使用 chrome 扩展来做到这一点,但我建议您查看Selenium Browser Automation
It will also help you do the same in multiple browser instead of just chrome.
它还可以帮助您在多个浏览器中执行相同的操作,而不仅仅是 chrome。