简体   繁体   English

Google chrome扩展程序cookie帮助!

[英]Google chrome extension cookie help!

I need my chrome extension to read a value of a cookie set by a rails app, I did just try and use plain javascript to achieve this but kept getting the result of the cookie as null. 我需要我的chrome扩展程序来读取Rails应用设置的cookie值,我只是尝试并使用普通的javascript来实现这一点,但一直将cookie的结果保持为null。

As I understand it you have to use the Chrome cookie API to read them? 据我了解,您必须使用Chrome cookie API来读取它们? I'm totally clueless when it comes to chrome extensions, I'm struggling with what the structure should be for the extensions ie A Background page, popup or a content script?? 对于Chrome扩展程序,我一无所知,我正在为扩展程序的结构而苦苦挣扎,例如,背景页面,弹出窗口或内容脚本?

Ideally I want the popup.html to iterate through the values in the cookie that is sent, so I would strip the cookie for the values which would then be written to the popup.html file. 理想情况下,我希望popup.html遍历发送的Cookie中的值,因此我将删除cookie中的值,然后将其写入popup.html文件。

Any ideas or suggestions where to start? 任何想法或建议从哪里开始?

Currently the best (the simplest) way to get site cookies in extension is like this: 当前,最好的(最简单的)方式来获取站点cookie如下:

chrome.cookies.get({ url: 'http://example.com', name: 'somename' },
  function (cookie) {
    if (cookie) {
      console.log(cookie.value);
    }
    else {
      console.log('Can\'t get cookie! Check the name!');
    }
});

So now you don't need content script for this but don't forget to include permissions into manifest: 因此,现在您不需要此内容脚本,但不要忘记在清单中包含权限:

"permissions": [
  "cookies",
  "*://*.example.com/*"
]

The only way you can read your sites cookies is by Content Scripts . 读取站点cookie的唯一方法是通过内容脚本

Since you wanted to do that in a popup.html page, you have two choices: 由于您想在popup.html页面中执行此操作,因此有两种选择:

  1. Tabs API - chrome.tabs.executeScript Tabs API- chrome.tabs.executeScript
  2. Messaging API - chrome.tabs.sendRequest 消息传递API- chrome.tabs.sendRequest

Once you are in the content script, you can communicate to that pages Cookies. 一旦进入内容脚本,就可以与该页面交流Cookie。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM