gpt4 book ai didi

javascript - 从 Firefox 扩展 (XUL) 读取网页 cookie

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:49:57 26 4
gpt4 key购买 nike

我正在为 Firefox 浏览器创建一个扩展。我想读取一个由 HTML 页面在 XUL 文件中使用 JavaScript 设置的 cookie。可能吗?

我尝试使用 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);
}

你能帮帮我吗?提前致谢。

最佳答案

这些代码片段可能会有所帮助

///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...

}
}

///如果你想通过域名读取cookies你可以使用这个代码...

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);
}

}

希望这些会有所帮助:)

关于javascript - 从 Firefox 扩展 (XUL) 读取网页 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1523137/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com