Google chrome extension 打开带有链接的新选项卡的Chrome扩展

Google chrome extension 打开带有链接的新选项卡的Chrome扩展,google-chrome-extension,Google Chrome Extension,我正在尝试从chrome扩展打开一个新选项卡。我在这里搜索了一下,但没有找到解决问题的方法 我只是想让它打开一个按钮点击标签 清单 { “清单版本”:2 popup.html: <!DOCTYPE html> <html> <head> ... </head> <body> <div id="main" class="container"> <button class="bt

我正在尝试从chrome扩展打开一个新选项卡。我在这里搜索了一下,但没有找到解决问题的方法

我只是想让它打开一个按钮点击标签

清单 { “清单版本”:2

popup.html:

  <!DOCTYPE html>
<html>
  <head>
    ...
  </head>
  <body>
    <div id="main" class="container">
      <button class="btn btn-success" id="doItBtn">Do it NOW!        
  </body>
</html>
我试着不把代码放在backgorund.js中,而是放在简单的脚本标记中,但没有成功

我做错了什么

问候,,
Ido

您不需要背景脚本。请将您的
background.js
重命名为
popup.js
,在
popup.html
文件末尾的
标记前面包含
,然后删除
背景
清单文件中的条目。我得到:未捕获类型错误:document.getElementsById不是HTMLDocument中的函数。请使用不存在的
document.getElementById
而不是
getElementsById
  <!DOCTYPE html>
<html>
  <head>
    ...
  </head>
  <body>
    <div id="main" class="container">
      <button class="btn btn-success" id="doItBtn">Do it NOW!        
  </body>
</html>
document.addEventListener('DOMContentLoaded', function () {
    var btn = document.getElementsById("doItBtn");
    btn.addEventListener('click', function () {
        chrome.tabs.create({active: true, url: "http://www.ynet.co.il"});
    });
});