4

我一直在花費我的最後三天試圖獲得此項工作,因此請不要回復鏈接到Chrome API或類似的東西。我正在開發一個與mySql數據庫通信的Chrome擴展,以獲得用戶最喜歡的網站,以及每個網站的登錄名/密碼(比如說Facebook的fbuser fbpass,谷歌的gguser ggpass ... )帶有標籤的Chrome擴展程序

所以擴展的目標是要打開一個新標籤將每一個網站,並做登錄。

我配合建立與數據庫通信,我可以成功地打開新標籤頁與網站,但我堆放在插入用戶名和各網站上的密碼。我的問題是我不能使用document.getElementsById('input')...因爲它返回我的彈出窗口中的元素,而不是在網頁上。

所以,我讀過有關chrome.tabs.executeScript(),和我想要使用它(似乎這就是我需要用來訪問網頁的DOM)。

所以我使用的是一樣的東西:

chrome.tabs.create({"url": web[i],"selected":false},function(tab) { 
     chrome.tabs.executeScript(tab.id, {file:"write.js"}, function(){ 
     }); 
     }); 

與 「write.js」 必須做的自動填充。

所以從這裏開始我的問題:

我有一個「警報(」我的「);」 write.js中的消息,並且它從不顯示(所以函數的其餘部分從未被調用過)。 (我在互聯網上發現了這個函數「write.js」)。

在這裏,我會後我的彈出HTML,清單和JS的部分,也許我有,因爲我已經試過了我見過的大多數的例子,而不是爲我工作在那裏改變一些東西。

manifest.json的:

{ 
    "name": "MyExtension", 
    "version": "1.0", 
    "description": "My chrome extension", 
    "icons":{ 
     "128":"icon_128.png" 
    }, 
    "browser_action": { 
     "default_icon": "icon.png", 
     "popup": "consulta_empleados.html" 
    }, 
    "permissions": [ 
    "http://*/*", 
    "https://*/*" 
    ] 
} 

consulta_empleados.html(彈出):

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
<title>Consulta Registro con AJAX</title> 
<script src="ajax.js"></script> 
</head> 
<body> 
<form name="form1" method="post" > 
    <label> 
    <div align="center">Email<br> 
    <input type="text" name="email" id="email"> 
    </div> 
    </label> 
    <p align="center"> 
    <label> 
    <input type="submit" name="open" id="open" value="Log In" onClick="runScript()"> 
    </label> 
    </p> 
</form> 
<div id="resultado"></div> 
<div id="prova"></div> 
</body> 
</html> 

文件ajax.js:

function runScript(){ 
     ... 
     //here I fill a vector with the usernames, the websites and the passwords from the data coming from my database. var num is the number of websites 
     ... 
    var i=0; 
    while(i<num){ 
    chrome.tabs.create({"url": web[i],"selected":false},function(tab) { 
     chrome.tabs.executeScript(tab.id, {file: "write.js"}, function(){ 

     }); 
     }); 
    i++; 
    } 

} 

write.js: 
function write() { 
    ... 
     alert("I'm in"); 
    ...  
} 
write(); 

在此先感謝。

+0

您是否嘗試過使用內容腳本,而不是'executeScript'?值得一試。如果它不起作用,請告訴我 - 我會嘗試調試您的擴展。 –

回答

0

恕我直言,而不是「選擇」:假的,你需要「激活」:假

也可以thest這種通過增加chrome.tabs.executeScript之前另一個警報,看看是否chrome.tabs.create回調是被稱爲

+0

我得到完全相同的結果。我也嘗試在chrome.tabs.create的回調函數中添加另一個警報,並且它從不顯示。 – Miquel

+0

所以你甚至沒有創建標籤?或者該選項卡已創建,但未調用回調? – Gavriel

+0

是的選項卡已創建,但未調用回調。 – Miquel

0

看看你的devtools'控制檯,必須有一些錯誤信息。 要使用contentscript,你需要tabs許可

{ 
    "name": "My extension", 
    ... 
    "permissions": [ 
    "tabs", "http://www.google.com/*" 
    ], 
    ... 
}