[英]Chrome Extension : fill the text fields
我的擴展程序應自動填寫我的gmail用戶名和密碼,但無法正常工作。
的manifest.json
{
"manifest_version": 2,
"name": "Click to execute",
"description": "Akshaya app",
"version": "1.0",
"icons": {
"48": "icon.png"
},
"permissions": [
"tabs", "<all_urls>"
],
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["https://www.facebook.com/*"],
"js": ["popup.js"]
}
]
}
彈出。 html文件
<button id="buttonSet">Set Value</button>
<script type="text/javascript" src="popup.js"></script>
popup.js
function setValue() {
var name ="username";
var pass = "password";
document.getElementById('username').value =name;
document.getElementById('pass').value =pass;
}
document.getElementById('buttonSet').addEventListener('click', setValue);
我從互聯網上獲得了所有代碼,而且我以前沒有使用過Google擴展程序的經驗,而且我用Google搜索了很多次以解決該問題,但找不到該問題的任何解決方案
希望我理解你對。 您可以在內容腳本和在popup.html(myscript.js)中運行的腳本之間使用消息傳遞 :
的manifest.json
{
"manifest_version": 2,
"name": "Click to execute",
"description": "Akshaya app",
"version": "1.0",
"icons": {
"48": "icon.png"
},
"permissions": [
"tabs", "<all_urls>"
],
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": ["https://www.facebook.com/*"],
"js": ["popup.js"]
}
]
}
popup.html。
<script type="text/javascript" src="myscript.js"></script>
<button id="buttonSet">Set Value</button>
<input type="text" id="username">
<input type="text" id="pass">
popup.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
//Take the fields of user and password from the DOM of facebook log-in page
document.getElementById('email').value=request.user;
document.getElementById('pass').value=request.pass;
});
myscript.js
window.onload=function(){
document.getElementById('buttonSet').addEventListener('click',function(){
chrome.tabs.query({}, function(tabs) {
for(var i = 0; i < tabs.length; i++) {
chrome.tabs.sendMessage(tabs[i].id, {user :document.getElementById('username').value,
pass :document.getElementById('pass').value}, function(response) {
});
}
});
});
}
在myscript.js中,您需要通過ID向您想要的內容腳本選項卡發送消息。 這是我向所有選項卡消息發送直到找到等待消息的選項卡的示例。 在結果的最后,第一個輸入將顯示“用戶名”,您也可以為第二個參數執行此操作。
祝好運 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.