我有問題找出我的代碼到底是什麼問題,因爲當我點擊圖標時,它說我運行background.js以及autofill.js。但是不會自動填充Gmail網站。這是我的第一個Chrome擴展以及使用JavaScript。我的最終目標是它可以自動填充所有站點(不僅僅是Gmail),並且能夠將所有密碼存儲/讀取到.txt文件中。另一件事是,當我嘗試運行此代碼是說,我的autofill.js文件有問題,並給我錯誤「未捕獲TypeError:無法設置null的屬性'值。這是爲autofill.js正確的下。評論//填寫用戶名和密碼 感謝你的時間來幫助我和任何的投入將幫助我,因爲我堅持撞牆了如何在我點擊圖標時創建自動填充用戶名/密碼的Google Chrome擴展程序?
manifest.json的:
{
"name": "Test",
"manifest_version": 2,
"version": "1.0",
"description": "This is a Chrome extension that will autofill passwords",
"browser_action": {
"default_icon": "icon.png",
"default_popup":"popup.html",
"default_title": "PasswordFill"
},
//*********************************************************************
//declaring the permissions that will be used in this extension
"permissions": ["*://*.google.com/", "tabs", "activeTab", "*://*.yahoo.com/"],
"background": {
"scripts": ["background.js", "autofill.js"]
},
//*********************************************************************
/* Content scripts are JavaScript files that run in the context of web pages. By using the standard Document Object Model (DOM), they can read details of the web pages the browser visits, or make changes to them */
"content_scripts": [
{
//Specifies which pages this content script will be injected into
"matches": ["https://accounts.google.com/ServiceLoginAuth"],
//The list of JavaScript files to be injected into matching pages
"js": ["autofill.js"], //was background.js
//Controls when the files at "js" are being injected
"run_at": "document_end",
"all_frames": true
}
]
}
背景.js:
console.log("Background.js Started .. "); //for debug purpose so i can see it in the console log
chrome.browserAction.onClicked.addListener(function (tab) { //Starts when User Clicks on ICON
chrome.tabs.executeScript(tab.id, {file: 'autofill.js'});
console.log("Script Executed .. "); // Notification on Completion
});
autofi ll.js:
console.log("Autofill.js Started .. "); //for debug purpose so i can see it in the console log
//define username and password
var myUsername = 'McAfee.sdp';
var myPassword = 'baskin310';
//finds the fields in your login form
var loginField = document.getElementById('Email');
var passwordField = document.getElementById('Passwd');
//fills in your username and password
loginField.value = myUsername;
passwordField.value = myPassword;
//automatically submits the login form
var loginForm = document.getElementById ('signIn');
loginForm.submit();