首页 > 解决方案 > 灰色的 Chrome 扩展程序仅可用于登录后 manifest.json 中声明的站点,否则在所有其他站点上显示为灰色

问题描述

我需要仅当用户登录到父站点(即 www.flyship.com)以及 manifest.json 中提到的站点时才可以使用扩展程序。

 "content_scripts": [
      {
        "matches": [
          "*://*.flyship.com/*",
          "*://app.yield.io/*",
          "*://*.indeedjobs.com/*"
        ],
        "js": ["scripts/content.js","scripts/index.js"]
      }
    ],

在 background.js 中,我正在检查用户是否使用此代码登录到 flyship.com

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse){
    if(request.login === "logged"){
      checkUser(request, sender, sendResponse);
      // checkforValidUrl();
    }
return true;
}
function checkUser(request, sender, sendResponse){
    chrome.cookies.get({url : "https://www.flyship.com/app", name: "csrftoken"}, function(cookie){
      const CSRF = cookie.value;
      sendResponse({token: CSRF});
      console.log(CSRF);
    });
  }

除了 manifest.json 中提到的站点之外,我需要在所有其他站点上将扩展显示为灰色,只有当用户登录到 flyship.com 时。

PS:我已经尝试过 declarativeContent 但它似乎对我不起作用,或者至少我无法根据我的需要推断它。以下是我检查网址的代码

function checkforValidUrl(){
  chrome.runtime.onInstalled.addListener(function() {
    chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
      chrome.declarativeContent.onPageChanged.addRules([
        {
          conditions: [
            new chrome.declarativeContent.PageStateMatcher({
              pageUrl: { urlContains: '*://*.flyship.com/*' },
            }),
            new chrome.declarativeContent.PageStateMatcher({
              pageUrl: { urlContains: '*://app.yield.io/*' }
            }),
            new chrome.declarativeContent.PageStateMatcher({
              pageUrl: { urlContains: 'https://www.indeedjobs.com/*' }
            })
          ],
          actions: [ new chrome.declarativeContent.ShowPageAction() ]
        }
      ]);
    });
  });
}

自从 1 周以来,我一直在努力解决这个问题,阅读文档等等。在这方面有礼貌的帮助会非常有帮助。

标签: javascriptgoogle-chromegoogle-chrome-extensionbrowser-extension

解决方案


推荐阅读