2013-12-16 4 views
0

처음으로 사용자에게 이메일과 비밀번호를 묻는 작업 자바 스크립트가 있습니다. 이후에 사용자가 해당 웹 페이지를 다시 방문 할 때마다 사용자 이름과 암호가 자동 완성됩니다. 내 HTML 코드에서는 JSFiddle.net에서 checkCookie() 함수를 호출해야한다.Chrome 확장 프로그램에서 자동 완성 자바 스크립트 실행

클릭하면 자동 완성 JS가 실행되는 Chrome 확장 프로그램을 만들고 싶습니다. 이 코드가 있지만 확장명을 클릭하면 출력이 없습니다. 아무도 내가 뭘 잘못하고 있는지 제안 해 줄 수 없어요.

// manifest.json 

{ 
    "manifest_version": 2, 

    "name": "Auto-fill Passwords", 
    "description": "This extension autofill password on click.", 
    "version": "1.0",  
    "browser_action": { 
    "default_icon": "icon.png" 
    }, 

    "background": { 
    "scripts": ["background.js"] 
    } 
} 




// background.js 
chrome.browserAction.onClicked.addListener(function (tab) 
{ //Fired when User Clicks ICON 

    chrome.tabs.executeScript(tab.id, { 
     "file": "autofill.js" 
    }, function() { 

    function setCookie(c_name, value, exdays) { 
    var exdate = new Date(); 
    exdate.setDate(exdate.getDate() + exdays); 

      var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString()); 
      document.cookie = c_name + "=" + c_value; 

     } 

function checkCookie() { 
var username = prompt("Please enter your name:", ""); 
var n= getCookie("username",username); 
if(n) 
{ 
    alert("UserID Match! The user's password is " +n) ; 
} 
else 
{ 
    //alert("UserID password doesnot exist "); 
    var password = prompt("Please enter your password:", ""); 
       username += "!"; 
     username += password; 
alert("Username = "+ username); 
setCookie("username", username, 365); 

} 

} 


     console.log("Script Executed .. "); // Notification on Completion 
    }); 
} 
}); 


//autofill.js 
alert("Check cookie function is called"); 
checkCookie(); 
+0

앞에서 콘텐츠 스크립트의 사용을 사용했지만 어디 선가 읽고 이 작업을 위해 background.js를 사용하는 것이 더 좋습니다. 좋은 제안을 찾고 있습니다. 읽어 주셔서 감사합니다 –

답변

1

우선, 당신은 당신의 background.js에 너무 많은 괄호가 있습니다. autofill.js 안에 setCookie()checkCookie() 함수를 선언하는 것이 좋습니다 (주입 된 스크립트 내에서 호출하려는 경우). 그렇지 않으면 콜백 함수에서 실제로 함수를 선언하기 전에 autofill.js 스크립트를 checkCookie()으로 실행하는 것은 의미가 없습니다. 매니페스트 파일에 필수 권한을 지정하지 않았습니다. 나는 당신의 코드를 조금 바꿨다. 이제 작동합니다. 그러나 getCookie() 함수를 지정하지 않았으므로 코드를 사용하여 주석을 달았습니다.

의 manifest.json :

{ 
    "manifest_version": 2, 

    "name": "Auto-fill Passwords", 
    "description": "This extension autofill password on click.", 
    "version": "1.0", 
    "permissions": [ 
    "activeTab", 
    "tabs" 
    ],  
    "browser_action": { 
    "default_icon": "icon.png" 
    }, 
    "background": { 
    "scripts": ["background.js"] 
    } 
} 

background.js :

chrome.browserAction.onClicked.addListener(function (tab) { //Fired when User Clicks ICON 

    chrome.tabs.executeScript(tab.id, { 
    "file": "autofill.js" 
    }, function() { 

    alert(); 

    }); 

    console.log("Script Executed .. "); // Notification on Completion 
}); 

autofill.js :

alert("Check cookie function is called"); 

function setCookie(c_name, value, exdays) { 
    var exdate = new Date(); 
    exdate.setDate(exdate.getDate() + exdays); 

    var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString()); 
    document.cookie = c_name + "=" + c_value; 

} 

function checkCookie() { 
    var username = prompt("Please enter your name:", ""); 
/* 
    var n= getCookie("username",username); 

if(n) { 
    alert("UserID Match! The user's password is " +n) ; 
    } else { 

    //alert("UserID password doesnot exist "); 
    var password = prompt("Please enter your password:", ""); 
    username += "!"; 
    username += password; 
    alert("Username = "+ username); 
    setCookie("username", username, 365); 
*/ 
} 

checkCookie(); 
+0

답장을 보내 주셔서 감사합니다. 분명히 주말에 이걸 시험해보고 알려줄거야. 대답 해줘서 고마워 –