3

在我的弹出页面中,我有一个 iframe 标签,里面有一个按钮。单击此按钮时,将调用包含在 iframe 脚本中的函数。此函数包含对 的调用chrome.tabs.create({ url: "..."});,但我不断收到“Uncaught TypeError: Cannot call method 'create' of undefined”。我想原因是我在 iframe 中,而不是在弹出页面本身中。

有没有办法克服这种晦涩难懂的问题并从 iframe 中创建一个新选项卡?

编辑

这是我的代码。

以下内容被加载到弹出页面的 iframe 中:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <link rel="stylesheet" type="text/css" href="style/frame_style.css" media="screen" />
  <script src='jquery-1.8.3.js'></script>
  <script src='scripts/frame_functions.js'></script>
</head>

<body>
  <div>
    <button value="Click to open Google in a new tab" class='new_tab_bt'></button>
  </div>
</body>
</html>

frame_functions.js:

$(document).on("click", '.new_tab_bt', function(){
  openNewTab(); 
});

function openNewTab()
{
chrome.tabs.create({ url: "https://www.google.com" });
}
4

1 回答 1

0

您的代码无需任何更改即可工作,我已添加工作代码作为参考;您的目标源 html 页面在哪里,您是iframe如何从浏览器操作弹出窗口中引用它的?如果这不能解决分享,您是如何iframe从浏览器操作弹出窗口中引用您的。

工作版本

清单.json

简单的代码,注册browser action

{
    "name": "Iframe Holder",
    "description": "http://stackoverflow.com/questions/14429598/chrome-extension-create-tab-from-within-an-iframe-in-popup",
    "version": "1",
    "manifest_version": 2,
    "browser_action": {
        "default_popup": "popup.html",
        "default_title": "Iframe."
    }
}

popup.html

popup在浏览器操作中包含目标页面。

<html>

    <body>
        <iframe width="300" height="300" src="iframe.html"></iframe>
    </body>

</html>

iframe.html

使用您的代码并消除不相关的 css 文件等

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script src='jquery.js'></script>
        <script src='iframe.js'></script>
    </head>

    <body>
        <div>
            <button value="Click to open Google in a new tab" class='new_tab_bt'></button>
        </div>
    </body>

</html>

iframe.js

使用您的代码没有修改

$(document).on("click", '.new_tab_bt', function () {
    openNewTab();
});

function openNewTab() {
    chrome.tabs.create({
        url: "https://www.google.com"
    });
}
于 2013-01-21T08:33:52.687 回答