代码之家  ›  专栏  ›  技术社区  ›  Dolphin

为什么谷歌chrome扩展后台没有收到选项页面的消息

  •  0
  • Dolphin  · 技术社区  · 4 年前

    我现在正在开发一个google chrome扩展。在选项页面中,我想向后台发送一条消息,这是选项页面代码:

    gotoReddwarfAccess() {
          debugger;
          if(this.reddwarfPassword && this.reddwarfUserName){
            chromeCall('storage.local.set',{
              reddwarf_user_name: this.reddwarfUserName,
              reddwarf_password: this.reddwarfPassword
            })
            .then((res) =>{
              chrome.runtime.sendMessage({ action: 'reddwarf_authorize' })
            });
          }
        },
    

    在保存 username 和 password 成功后,选项页面将发送 reddwarf_authorize 向后台发送消息。在背景js中,我收到如下消息:

    chrome.runtime.onMessage.addListener(function (request, sender, sendReponse) {
        debugger;
        switch (request.action) {
            case 'reddwarf_authorize':
                alert("reddwarf auth");
                let authorize_url = `https://api.example.top/oauth2/authorize/?response_type=token`
                window.open(authorize_url)
                sendReponse()
                break;
            default:
                break;
        }
        return true
    })
    

    当我运行这段代码时,在后台没有收到任何消息 reddwarf-auth.js ,后台逻辑没有触发。代码问题在哪里?我错过什么了吗?我该怎么做才能让它工作?这是我的 manifiest.json 在此扩展中:

    {
      "name" : "translate" ,
      "description" : "" ,
      "manifest_version" : 2 ,
      "version" : "6.4.12.1" ,
      "homepage_url" : "https://github.com/jiangxiaoqiang/crx-selection-translate" ,
      "content_security_policy" : "script-src 'self' 'unsafe-eval'; object-src 'self'" ,
      "incognito": "split" ,
      "icons" : {
        "128" : "/logo.png"
      } ,
      "author" : "Milk Lee <example@qq.com>" ,
      "permissions" : [
        "<all_urls>" ,
        "contextMenus" ,
        "storage" ,
        "clipboardWrite" ,
        "clipboardRead" ,
        "webRequest",
        "webRequestBlocking"
      ] ,
      "background" : {
        "scripts" : [
          "/bundle/commons3.js" ,
          "/bundle/bg.js"
        ]
      } ,
      "content_scripts" : [
        {
          "matches" : [
            "http://*/*" ,
            "https://*/*" ,
            "file:///*"
          ] ,
          "css" : [
            "/bundle/commons1.js.css"
          ] ,
          "js" : [
            "/bundle/commons3.js" ,
            "/bundle/commons2.js" ,
            "/bundle/commons1.js" ,
            "/bundle/content.js"
          ] ,
          "all_frames" : true ,
          "run_at" : "document_start"
        }
      ] ,
      "options_page" : "/options/index.html" ,
      "options_ui" : {
        "page" : "/options/index.html" ,
        "chrome_style" : true
      } ,
      "browser_action" : {
        "default_icon" : {
          "19" : "/logo.png" ,
          "38" : "/logo.png"
        } ,
        "default_popup" : "/popup/index.html"
      } ,
      "commands" : {
        "_execute_browser_action" : {
          "suggested_key" : {
            "default" : "Ctrl+Q" ,
            "mac" : "MacCtrl+Q"
          }
        } ,
        "translate" : {
          "suggested_key" : {
            "default" : "Alt+A"
          } ,
          "description" : ""
        }
      } ,
      "minimum_chrome_version" : "29" ,
      "web_accessible_resources" : [
        "/bundle/*.woff" ,
        "/content-scripts/web/embed/*",
        "/pdf-viewer/*"
      ]
    }
    
    0 回复  |  直到 4 年前