从 Firefox 扩展 (XUL) 读取网页 cookie
我正在为 Firefox 浏览器创建一个扩展。我想读取 XUL 文件中使用 JavaScript 由 HTML 页面设置的 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);
}
你能帮我吗?提前致谢。
I'm creating an extension for the Firefox browser. I would like to read a cookie which was set by an HTML page using JavaScript in the XUL file. Is it possible?
I tried using document.cookie
, but it doesn't work:
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);
}
Could you help me? Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这些代码片段可能会有所帮助
///如果您想通过域名读取 cookie,您可以使用此代码...
希望这些会有所帮助:)
These code snippets may help
///If you want to read cookies by domain name you can use this code...
Hope these will help :)
您可能想使用 nsICookieService 接口:https://developer.mozilla .org/en/Code_snippets/Cookies
(通过 Mozilla 的附加组件进行有用搜索找到开发者中心:https://addons.mozilla.org/ en-US/developers/search?q=cookie)
You probably want to use the
nsICookieService
interface: https://developer.mozilla.org/en/Code_snippets/Cookies(Found via the helpful search on Mozilla's Add-on Developer Hub: https://addons.mozilla.org/en-US/developers/search?q=cookie)
您可能还想查看可与 cookie 配合使用的现有扩展,例如 FireCookie。
You also might want to look at existing extensions that work with cookies, such as FireCookie.
至少有两种不同的方法可以做到这一点:
首先,如果您只是想与 Firefox cookie 存储进行交互,您可以使用 nsICookieManager 和 nsICookieManager2 接口用于查询、添加和删除 cookie 。
其次,如果您更喜欢
document.cookie
方法,并且您想要处理浏览器中已加载的文档,您可以这样做,但要记住的重要一点是获取正确的文档以供使用。当您在 XUL 代码中简单地使用document
时,您指的是与您正在运行的浏览器窗口关联的文档对象。多个页面可能会加载到此窗口中的不同选项卡中,每个页面都有自己的文档对象。因此,您首先需要找到您感兴趣的文档对象。例如,执行此操作的一种方法是注册以获取页面加载通知,然后在页面加载时检查页面以查看它们是否已加载。的兴趣。例如,您的 XUL 代码可能如下所示:通过这种方法,您可以使用您熟悉的机制仅与与该页面关联的那些 cookie 进行交互,而不必担心处理与完全不同页面关联的 cookie。
There are at least two distinct ways of doing this:
Firstly, if you simply want to interact with the Firefox cookie store, you can use the nsICookieManager and nsICookieManager2 interfaces to query, add and remove cookies.
Secondly, if you're more comfortable with the
document.cookie
approach, and you want to deal with documents which are already loaded in the browser, you can do this, but the important thing to remember is to get the right document to work with. When you simply usedocument
in XUL code, you are referring to the document object associated with the browser window in which you are running. Several pages might be loaded in different tabs within this window, each of which will have its own document object. As a result, you first need to find the document object that you are interested in. One way of doing this, for example, is to register to be notified of pages loads, and then to examine pages as they load to see whether they are of interest. For example, your XUL code might look like this:With this approach you can interact with just those cookies associated with that page using the mechanisms with which you are familiar, and without worrying about dealing with cookies associated with completely different pages.