微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

浏览器扩展消息传递-在popup.js中获得空响应

如何解决浏览器扩展消息传递-在popup.js中获得空响应

我不确定为什么,但是当我尝试在前端的浏览器扩展中发送响应时,我在控制台中得到一个空的响应对象。我正在使用vue和polyfill创建它,使其可以在Firefox和chrome上运行。这是代码

background.js

import nedb from "nedb-promises";

const db = {};
let storedSettings = {};

db.settings = nedb.create({
  autoload: true,filename: browser.runtime.getURL("settings.db")
});

browser.runtime.onMessage.addListener( async (message,sender,sendResponse) => {
  console.log(message);
  if( message.action === "readSettings" ){
    readSettings();
    console.log(storedSettings); // here the variable is defined and contains the info to send
    return new Promise( (resolve) => {
      console.log(storedSettings); // also here the variable contains the info to send
      resolve(storedSettings)
    }); 
  }
});

popup.js vue组件

export default {
  name: 'Popup',data() {
    return {
      proxyActive: false
    }
  },created() {
    browser.runtime.sendMessage({action: "readSettings"}).then( (response) => {
      console.log(response); // the response is empty 
    });
  },mounted() {
    
  },computed: {

  },methods: {

  }
}

我该如何解决

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。