[英]Opening a new tab from text entry from chrome extension
I'm trying to make a chrome extension that takes input, adds it to a URL and then opens the URL in a new tab. 我试图制作一个带输入的chrome扩展名,将其添加到URL,然后在新选项卡中打开URL。 So far I've gotten my popup.html to work on its own but I'm struggling to get the code working with the extension.
到目前为止,我已经使popup.html能够独立工作,但是我正在努力使代码与扩展一起工作。
{
"manifest_version": 2,
"name": "SubSearch",
"description": "A simple way to go to subreddits",
"version": "0.1",
"browser_action":
{
"default_popup": "popup.html"
},
"permissions": ["tabs"]
}
Here is my manifest.json 这是我的manifest.json
<!DOCTYPE html5>
<html>
<body>
<script>
function process()
{
var url="http://reddit.com/r/" + document.getElementById("url").value;
location.href=url;
return false;
}
</script>
<form onSubmit="return process();">
<input type="text" name="url" id="url"> <input type="submit" value="go">
</form>
</body>
</html>
and here is my popup.html. 这是我的popup.html。 I saw this page but I'm struggling to apply the chrome.tabs.create method to my current code.
我看到了此页面,但是我正在努力将chrome.tabs.create方法应用于当前代码。 Any help is appreciated, still new to js and chrome dev!
感谢您的任何帮助,对于js和chrome开发人员来说还是新手!
Edit: I see that inline javascript is not executable in chrome. 编辑:我看到嵌入式javascript无法在chrome中执行。 Is there a way I can write this (possibly in a separate .js file) to allow me to achieve my goal?
有什么办法可以编写此代码(可能在单独的.js文件中)以实现目标?
Since you can't use inline Javascript, you need to write the Javascript code in a separate source file. 由于您不能使用内联Javascript,因此需要在单独的源文件中编写Javascript代码。 For instance:
例如:
popup.html popup.html
<html>
<body>
<input type="text" name="url" id="url"><button id="myButton">go</button>
<script src="popup.js"></script>
</body>
</html>
and in a separate file: 并在一个单独的文件中:
popup.js popup.js
function process()
{
var url="http://reddit.com/r/" + document.getElementById("url").value;
chrome.tabs.create({url:url}); //create a new tab
}
document.getElementById("myButton").onclick = process;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.