简体   繁体   English

从Chrome扩展程序的文本输入中打开新标签页

[英]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.

相关问题 使用Chrome扩展程序从活动标签页中的链接元素打开带有URL的新标签页 - Opening new tab with URL from link element in active tab using Chrome Extension 从Chrome新标签页扩展名接收postMessage - Receive postMessage from chrome new tab extension 在 Google Chrome 扩展程序上打开一个新标签 - Opening a new tab on Google Chrome Extension Chrome扩展程序:未在新标签页中打开URL - Chrome Extension: URL not opening in new tab Chrome 扩展:webRequest 重定向到现有选项卡,无需打开新选项卡 - Chrome Extension: webRequest Redirect to Existing Tab, Without Opening New Tab 简单的JavaScript Chrome扩展程序-打开URL-从输入文本中修改 - Simple JavaScript Chrome Extension - Opening a URL - modified from Input Text 事件发生在同一选项卡中,同时在 Firefox 扩展中打开从活动选项卡到新选项卡的链接 - Event occurring in same tab, while opening a link from active tab to new tab in Firefox extension 如何在新标签页的Chrome扩展程序的多功能框中窃取焦点? - How to steal focus from the omnibox in a Chrome extension on the new tab page? Firefox/Chrome 扩展 - 阻止新标签 - Firefox/Chrome extension - block from new-tab Chrome 新标签页扩展从地址栏窃取焦点 - Chrome New Tab Page extension steal focus from the address bar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM