michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: const { Cc, Ci, Cu } = require("chrome"); michael@0: const gcli = require("gcli/index"); michael@0: const cookieMgr = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2); michael@0: michael@0: /** michael@0: * The cookie 'expires' value needs converting into something more readable michael@0: */ michael@0: function translateExpires(expires) { michael@0: if (expires == 0) { michael@0: return gcli.lookup("cookieListOutSession"); michael@0: } michael@0: return new Date(expires).toLocaleString(); michael@0: } michael@0: michael@0: /** michael@0: * Check if a given cookie matches a given host michael@0: */ michael@0: function isCookieAtHost(cookie, host) { michael@0: if (cookie.host == null) { michael@0: return host == null; michael@0: } michael@0: if (cookie.host.startsWith(".")) { michael@0: return host.endsWith(cookie.host); michael@0: } michael@0: else { michael@0: return cookie.host == host; michael@0: } michael@0: } michael@0: michael@0: exports.items = [ michael@0: { michael@0: name: "cookie", michael@0: description: gcli.lookup("cookieDesc"), michael@0: manual: gcli.lookup("cookieManual") michael@0: }, michael@0: { michael@0: name: "cookie list", michael@0: description: gcli.lookup("cookieListDesc"), michael@0: manual: gcli.lookup("cookieListManual"), michael@0: returnType: "cookies", michael@0: exec: function(args, context) { michael@0: let host = context.environment.document.location.host; michael@0: if (host == null || host == "") { michael@0: throw new Error(gcli.lookup("cookieListOutNonePage")); michael@0: } michael@0: michael@0: let enm = cookieMgr.getCookiesFromHost(host); michael@0: michael@0: let cookies = []; michael@0: while (enm.hasMoreElements()) { michael@0: let cookie = enm.getNext().QueryInterface(Ci.nsICookie); michael@0: if (isCookieAtHost(cookie, host)) { michael@0: cookies.push({ michael@0: host: cookie.host, michael@0: name: cookie.name, michael@0: value: cookie.value, michael@0: path: cookie.path, michael@0: expires: cookie.expires, michael@0: secure: cookie.secure, michael@0: httpOnly: cookie.httpOnly, michael@0: sameDomain: cookie.sameDomain michael@0: }); michael@0: } michael@0: } michael@0: michael@0: return cookies; michael@0: } michael@0: }, michael@0: { michael@0: name: "cookie remove", michael@0: description: gcli.lookup("cookieRemoveDesc"), michael@0: manual: gcli.lookup("cookieRemoveManual"), michael@0: params: [ michael@0: { michael@0: name: "name", michael@0: type: "string", michael@0: description: gcli.lookup("cookieRemoveKeyDesc"), michael@0: } michael@0: ], michael@0: exec: function(args, context) { michael@0: let host = context.environment.document.location.host; michael@0: let enm = cookieMgr.getCookiesFromHost(host); michael@0: michael@0: let cookies = []; michael@0: while (enm.hasMoreElements()) { michael@0: let cookie = enm.getNext().QueryInterface(Ci.nsICookie); michael@0: if (isCookieAtHost(cookie, host)) { michael@0: if (cookie.name == args.name) { michael@0: cookieMgr.remove(cookie.host, cookie.name, cookie.path, false); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: }, michael@0: { michael@0: item: "converter", michael@0: from: "cookies", michael@0: to: "view", michael@0: exec: function(cookies, context) { michael@0: if (cookies.length == 0) { michael@0: let host = context.environment.document.location.host; michael@0: let msg = gcli.lookupFormat("cookieListOutNoneHost", [ host ]); michael@0: return context.createView({ html: "" + msg + "" }); michael@0: } michael@0: michael@0: for (let cookie of cookies) { michael@0: cookie.expires = translateExpires(cookie.expires); michael@0: michael@0: let noAttrs = !cookie.secure && !cookie.httpOnly && !cookie.sameDomain; michael@0: cookie.attrs = (cookie.secure ? "secure" : " ") + michael@0: (cookie.httpOnly ? "httpOnly" : " ") + michael@0: (cookie.sameDomain ? "sameDomain" : " ") + michael@0: (noAttrs ? gcli.lookup("cookieListOutNone") : " "); michael@0: } michael@0: michael@0: return context.createView({ michael@0: html: michael@0: "", michael@0: data: { michael@0: options: { allowEval: true }, michael@0: cookies: cookies, michael@0: onclick: context.update, michael@0: ondblclick: context.updateExec michael@0: } michael@0: }); michael@0: } michael@0: }, michael@0: { michael@0: name: "cookie set", michael@0: description: gcli.lookup("cookieSetDesc"), michael@0: manual: gcli.lookup("cookieSetManual"), michael@0: params: [ michael@0: { michael@0: name: "name", michael@0: type: "string", michael@0: description: gcli.lookup("cookieSetKeyDesc") michael@0: }, michael@0: { michael@0: name: "value", michael@0: type: "string", michael@0: description: gcli.lookup("cookieSetValueDesc") michael@0: }, michael@0: { michael@0: group: gcli.lookup("cookieSetOptionsDesc"), michael@0: params: [ michael@0: { michael@0: name: "path", michael@0: type: { name: "string", allowBlank: true }, michael@0: defaultValue: "/", michael@0: description: gcli.lookup("cookieSetPathDesc") michael@0: }, michael@0: { michael@0: name: "domain", michael@0: type: "string", michael@0: defaultValue: null, michael@0: description: gcli.lookup("cookieSetDomainDesc") michael@0: }, michael@0: { michael@0: name: "secure", michael@0: type: "boolean", michael@0: description: gcli.lookup("cookieSetSecureDesc") michael@0: }, michael@0: { michael@0: name: "httpOnly", michael@0: type: "boolean", michael@0: description: gcli.lookup("cookieSetHttpOnlyDesc") michael@0: }, michael@0: { michael@0: name: "session", michael@0: type: "boolean", michael@0: description: gcli.lookup("cookieSetSessionDesc") michael@0: }, michael@0: { michael@0: name: "expires", michael@0: type: "string", michael@0: defaultValue: "Jan 17, 2038", michael@0: description: gcli.lookup("cookieSetExpiresDesc") michael@0: }, michael@0: ] michael@0: } michael@0: ], michael@0: exec: function(args, context) { michael@0: let host = context.environment.document.location.host; michael@0: let time = Date.parse(args.expires) / 1000; michael@0: michael@0: cookieMgr.add(args.domain ? "." + args.domain : host, michael@0: args.path ? args.path : "/", michael@0: args.name, michael@0: args.value, michael@0: args.secure, michael@0: args.httpOnly, michael@0: args.session, michael@0: time); michael@0: } michael@0: } michael@0: ];