Javascript 如何在google chrome扩展中检测到我已登录到另一个网站?

Javascript 如何在google chrome扩展中检测到我已登录到另一个网站?,javascript,reactjs,google-chrome-extension,Javascript,Reactjs,Google Chrome Extension,我有一个web应用程序,我为它做了一个额外的google chrome扩展。如果我登录到该网站,如何在google chrome扩展中检测它,这样我就不必再次登录扩展。当我登录到站点时,我希望扩展检测到我已登录到站点并自动登录到扩展 我有以下错误: 无法为内容脚本加载JavaScript文件“content.js” manifest.json { "name": "EXTENSION", "options_page": "options.html", "background": {

我有一个web应用程序,我为它做了一个额外的google chrome扩展。如果我登录到该网站,如何在google chrome扩展中检测它,这样我就不必再次登录扩展。当我登录到站点时,我希望扩展检测到我已登录到站点并自动登录到扩展

我有以下错误:

无法为内容脚本加载JavaScript文件“content.js”

manifest.json

{
  "name": "EXTENSION",
  "options_page": "options.html",
  "background": {
    "page": "background.html"
  },

  "content_scripts": [{
    "matches": [ "*://*.app.com/*"  ],
    "js": [ "content.js" ],
    "all_frames": true
}],
  "browser_action": {
    "default_popup": "popup.html",
    "default_icon": "icon-34.png"
  },
  "icons": {
    "128": "icon-128.png"
  },
  "permissions": [
   "https://*/",
   "http://*/",
   "*://*.app.com/*",
    "storage"
  ],
  "version": "1.0",
  "manifest_version": 2,
  "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
}
content.js

if(chrome && chrome.storage){
  chrome.storage.sync.get('token', function(result){

    const item = result['access_token'];
    console.log(item);

    chrome.runtime.sendMessage(item, function(response) {
      console.log(response);
    });
  });
}


var port = chrome.runtime.connect({name: 'test'});
port.onMessage.addListener(function(msg, port) {
  console.log(msg);
});
port.postMessage('from-iframe');
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
  console.log('popup got', msg, 'from', sender);
  sendResponse('response');
});

var iframePort; another function

chrome.runtime.onConnect.addListener(function(port) {
    iframePort = port;
    port.onMessage.addListener(function(msg, port) {
        console.log(msg);
    });
    port.postMessage('from-popup');
});

render(
  <App/>,
  window.document.getElementById("app-container")
);
//src
    //js
      //background.js
      //options.js
      //popup.js
//background.html
//manifest.json
//content.js
//options.html
//popup.html
popup.js

if(chrome && chrome.storage){
  chrome.storage.sync.get('token', function(result){

    const item = result['access_token'];
    console.log(item);

    chrome.runtime.sendMessage(item, function(response) {
      console.log(response);
    });
  });
}


var port = chrome.runtime.connect({name: 'test'});
port.onMessage.addListener(function(msg, port) {
  console.log(msg);
});
port.postMessage('from-iframe');
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
  console.log('popup got', msg, 'from', sender);
  sendResponse('response');
});

var iframePort; another function

chrome.runtime.onConnect.addListener(function(port) {
    iframePort = port;
    port.onMessage.addListener(function(msg, port) {
        console.log(msg);
    });
    port.postMessage('from-popup');
});

render(
  <App/>,
  window.document.getElementById("app-container")
);
//src
    //js
      //background.js
      //options.js
      //popup.js
//background.html
//manifest.json
//content.js
//options.html
//popup.html
当我删除

"content_scripts": [{
    "matches": [ "*://*.app.com/*"  ],
    "js": [ "content.js" ],
    "all_frames": true
}]
分机正在加载

当用户已经登录到chrome扩展正在使用的网站时,我如何对使用chrome扩展的用户进行身份验证

这个答案有点宽泛,但最常见的身份验证方法通常是2,要么通过cookies,要么通过后端签名的令牌,然后告诉后端他正在与哪个用户通话

对于Cookie: 如果身份验证是使用cookies完成的,那么您只需要在扩展名
chrome.cookies
中使用cookies,这将允许您在后台脚本中使用cookies。或者,如果chrome扩展正在使用内容脚本,那么您可以直接调用
document.cookies
来检索身份验证cookie

对于令牌身份验证: 例如,如果您的应用程序恰好使用OAuth2,或者类似的实现,在该实现中,用户被授予访问令牌并存储在本地,以便对API进行身份验证,那么您可以通过调用保存令牌的位置来检索它

如果令牌保存在
localStorage
中,则可以使用
get
方法检索kep中的信息。
类似地,您可以访问
会话存储
,以防令牌保存在那里

您应该将content.js放在公用文件夹中,然后添加到其中 index.html

<script src="%PUBLIC_URL%/content.js"></script>

确保
content.js
manifest.json
位于同一文件夹中,或者在
manifest.json
中更改指向它的路径

  "content_scripts": [{
    "matches": [ "*://*.app.com/*"  ],
    "js": [ "PATH_TO_CONTENT/content.js" ], // Change this line
    "all_frames": true
  }],

为了检查您是否登录,上面的答案是正确的,但听起来您确实在寻找
无法加载JavaScript文件的原因。
这取决于该站点如何处理身份验证。如果是cookies,您的扩展可以在后台脚本中使用chrome.cookies API,或在内容脚本中使用document.cookie。添加更多相关细节,使问题更易于回答。@wOxxOm认证OAuth 2.0。访问本地存储中的令牌。您是否有用于访问localStorage或声明内容脚本的资料、示例或资料?两者都应该很容易找到。材料,示例我如何使用chrome扩展中另一个页面的localstorege中的日期并自动登录到扩展。我怀疑任何人都可以提供这些日期,而不会看到您的设置的更详细说明。这并不能解决我的问题:我写了一个错误:
无法加载JavaScript文件“content.js”对于内容脚本。
我认为问题在于身份验证。项目的文件结构是什么?另外,当您绑定项目时,它是否会生成
content.js
文件?你已经试过了吗?它不起作用。当我删除
“content\u scripts”:[{“matches”:[“*://*.app.com/*”],“js”:[“content.js”],“all\u frames”:true}]
扩展工作要转到下一个问题“我登录了吗”,我必须解决第一个问题。在这种情况下,你应该尝试这个答案中的解决方案!这是专门针对这个问题的,我将content.hs与manifest.json放在同一个文件夹中。但它不起作用