在contentScript中的firefox插件中设置document.cookie

时间:2015-06-18 14:21:21

标签: firefox-addon firefox-addon-sdk

我想在firefox addon contentScipt中设置一些cookie:

// Content Script ( js/firefox.js )

self.port.on("start", function() {
  var request;
  document.cookie = "name=value; path=/;";
  request = new XMLHttpRequest();
  request.open('GET', '/', true);
  request.send(null);
});

但是当我看到请求标题和cookie时,没有任何关于cookie'name'的信息。 是否可以在contentScipt中添加cookie?

主要插件脚本如下所示

// Main Addon Script ( js/index.js )

var data = require("sdk/self").data;
var button = require("sdk/ui/button/toggle").ToggleButton({
  id: "button",
  label: "button",
  icon: "./img/icon/16.png",
  onChange: handleChange
});

var panel = require("sdk/panel").Panel({
  contentURL: data.url("popup.html"),
  contentScriptFile: data.url("js/firefox.js"),
  position: button,
  onHide: handleHide
});

function handleChange(state) {
  if (state.checked) {
    panel.show({
      position: button
    });
  }
}

function handleHide() {
  button.state('window', {checked: false});
}

panel.on("show", function () {
  panel.port.emit("start");
});

1 个答案:

答案 0 :(得分:0)

尝试:

self.port.on("start", function() {
  var request;
  unsafeWindow.document.cookie = "name=value; path=/;";
  request = new XMLHttpRequest();
  request.open('GET', '/', true);
  request.send(null);
});