Chrome扩展程序无法在新标签

时间:2016-10-12 17:56:05

标签: javascript google-chrome google-chrome-extension

我有这段代码但只打开新标签页中的最后一个按钮

我的代码是 的manifest.json

{
    "manifest_version": 2,
    "name": "Open Links",  
    "description": "Open Links open all links",  
    "version": "1.0",    
    "permissions": ["tabs", "<all_urls>"],  

    "browser_action": { 
        "default_icon": {
            "19": "openall.png"
        },
        "default_popup": "popup.html"  
    },

    "icons": {
        "19": "openall.png"
    }
}

popup.html

<!doctype html>  
<html>  
    <head><title>Open Links</title></head>  
<body>  
    <button id="buttonclick">click</button>  
    <script src="popup.js"></script> 
</body>
</html>  

popup.js

function runTheScript() {
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
        chrome.tabs.executeScript(tabs[0].id, {file: "content_script.js"});
    });
}

document.getElementById('buttonclick').addEventListener('click', runTheScript);

和finaly content_script.js

function clickToOpen() {

    var newTab = document.getElementsByClassName("btn-detail");

    for (i = 0; i < 5; i++) { 
        clickNewTab = newTab[i];
        clickNewTab.click();
    }

}

clickToOpen();

我卡住了,我没有更多的想法。

我尝试过的网页代码 test.php的

<!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" />
<title>Test Page</title>
</head>

<body>

<div> 
    <form target="_blank" action="nextpage.php" method="post"> 
        <fieldset> 
            <input name="id" value="1" type="hidden"> 
            <input name="sid" value="18" type="hidden"> 
            <input type="submit" value="Get Page" class="btn-detail"> 
        </fieldset> 
    </form> 
</div>
<div> 
    <form target="_blank" action="nextpage.php" method="post"> 
        <fieldset> 
            <input name="id" value="21" type="hidden"> 
            <input name="sid" value="28" type="hidden"> 
            <input type="submit" value="Get Page" class="btn-detail"> 
        </fieldset> 
    </form> 
</div>
<div> 
    <form target="_blank" action="nextpage.php" method="post"> 
        <fieldset> 
            <input name="id" value="3" type="hidden"> 
            <input name="sid" value="38" type="hidden"> 
            <input type="submit" value="Get Page" class="btn-detail"> 
        </fieldset> 
    </form> 
</div>
<div> 
    <form target="_blank" action="nextpage.php" method="post"> 
        <fieldset> 
            <input name="id" value="4" type="hidden"> 
            <input name="sid" value="48" type="hidden"> 
            <input type="submit" value="Get Page" class="btn-detail"> 
        </fieldset> 
    </form> 
</div>


</body>
</html>

和nextpage.php

<!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" />
<title>Next Page</title>
</head>

<body>

<div> 
<h2>ID <?php echo $_POST['id']; ?></h2></div>
<div> 
<h2>SID <?php echo $_POST['sid']; ?></h2></div>


</body>
</html>

有任何建议打开新标签中的所有指定链接

1 个答案:

答案 0 :(得分:0)

使用window.open()和“_ tab”选项代替click(),在新标签页中打开它。