2009-10-06 2 views
3

Firefox 브라우저 용 확장 프로그램을 만들고 있습니다. XUL 파일에서 JavaScript를 사용하여 HTML 페이지에서 설정 한 쿠키를 읽고 싶습니다. 가능한가? Firefox 확장 기능 (XUL)에서 웹 페이지 쿠키 읽기

나는 document.cookie를 사용하여 시도했지만 작동하지 않습니다 : 당신은 날

function readCookie(name) { 
    var ca = document.cookie.split(';'); 
    var nameEQ = name + "="; 
    for(var i=0; i < ca.length; i++) { 
    var c = ca[i]; 
    while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces 
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length); 
    } 
    return ""; 
} 

function createCookie(name, value, days) { 
    if (days) { 
    var date = new Date(); 
    date.setTime(date.getTime()+(days*24*60*60*1000)); 
    var expires = "; expires="+date.toGMTString(); 
    } 
    else var expires = ""; 
    document.cookie = name+"="+value+expires+"; path=/"; 
} 

function eraseCookie(name) { 
    createCookie(name, "", -1); 
} 

도울 수 있을까요? 미리 감사드립니다.

답변

1

당신은 또한 다음과 같은 쿠키 작업을 기존의 확장,보고 할 수 있습니다를 당신은 단순히 간 원하는 경우

, 첫째 : FireCookie.

+0

답장을 보내 주셔서 감사합니다. xcode 형식의 확장 코드 bcoz를 보는 방법을 알고 있습니까 –

+0

새로운 질문을하기 시작했습니다 .... .xpi는 압축되어 있습니다. 추출기로 파일을 열고 http://en.wikipedia.org/wiki/XPInstall을보고 파일을 압축하고 확장명을 변경하십시오. –

1

는이 일을 적어도 두 가지 방법이 있습니다 Firefox 쿠키 저장소에서 작동하는 경우 nsICookieManagernsICookieManager2 인터페이스를 사용하여 쿠키를 쿼리, 추가 및 제거 할 수 있습니다.

두 번째로 document.cookie 접근법에 익숙하고 브라우저에 이미로드 된 문서를 처리하려는 경우이 작업을 수행 할 수 있지만 기억해야 할 중요한 사항은 작업. document을 XUL 코드에 사용하기 만하면 실행중인 브라우저 창과 연관된 문서 객체를 참조하게됩니다. 이 창의 각기 다른 탭에 여러 페이지가로드 될 수 있습니다. 각 탭에는 자체 문서 객체가 있습니다. 결과적으로 관심이있는 문서 객체를 먼저 찾아야합니다. 예를 들어,이 작업을 수행하는 한 가지 방법은 페이지로드 알림에 등록하고 페이지로드 여부를 확인하기 위해 페이지를 검사하는 것입니다 관심의 대상. 예를 들어, XUL 코드는 다음과 같습니다 당신은 당신이 잘 알고있는 메커니즘을 사용하여 해당 페이지와 관련된 단지 그 쿠키와 상호 작용할 수있는이 방법으로

function pageLoad (event) { 
    if (event.originalTarget instanceof HTMLDocument) { 
    var doc = event.originalTarget; 

    if (doc.URL.match(/^https?:\/\/[^\/]*\.example\.com\//)) { 
     ⋮ 
     doc.cookie = 'cookie_name' + "=" + value + ";expires=" + 
        (new Date(2050, 10, 23)).toGMTString(); 
     ⋮ 
    } 
    } 
} 

var appcontent = document.getElementById("appcontent"); // locate browser 

if (appcontent) 
    appcontent.addEventListener("load", function (e) { pageLoad(e); }, true); 

을하고와 관련된 쿠키 처리에 대한 걱정없이 전혀 다른 페이지.

14

이 코드 조각은 도움이 될 수

///By this method you can iterate throug each cookie by its name or value... 

function ReadCookie() 
{ 
var cookieMgr = Components.classes["@mozilla.org/cookiemanager;1"] 
     .getService(Components.interfaces.nsICookieManager); 

for (var e = cookieMgr.enumerator; e.hasMoreElements();) { 
var cookie = e.getNext().QueryInterface(Components.interfaces.nsICookie); 
    dump(cookie.host + ";" + cookie.name + "=" + cookie.value + "\n"); 

///Your code here to check your cookie or your implementation... 
///You can use cookie name or value to select your cookie... 

} 
} 

/// 당신은

function GetCookie(){ 
try 
{ 
alert("Getting Cookies"); 
    var ios = Components.classes["@mozilla.org/network/io-service;1"] 
      .getService(Components.interfaces.nsIIOService); 
var uri = ios.newURI("http://www.yoursite.com/", null, null); 

var cookieSvc = 
    Components.classes["@mozilla.org/cookieService;1"] 
      .getService(Components.interfaces.nsICookieService); 
var cookie = cookieSvc.getCookieString(uri, null); 

///Your logic here... 
} 
catch (errorInfo) 
{ 
    alert(errorInfo); 
} 

} 

다음은 :)

도움이 될 것입니다 희망 ...이 코드를 사용할 수있는 도메인 이름으로 쿠키를 읽고 싶다면
+0

이 답변을 주셔서 감사합니다. –

+0

Good gupta ji.Keep up. – PrateekSaluja