【问题标题】:Chrome Extension: How to assign global value from onMessage local scope functionChrome 扩展:如何从 onMessage 本地范围函数分配全局值
【发布时间】:2019-05-08 16:52:16
【问题描述】:

基本上,我要做的是从 popup.js 发送一条消息,方法是从输入字段中获取值,然后在 content_script 中的 onMessage 将这些值从本地范围分配给全局范围。我做不到!我可以在 onMessage 函数中关闭整个代码,但是要进入代码的下一个函数,我必须继续单击 chrome 扩展上的保存。如果有人比我对 chrome 扩展更了解这种方式,请查看我的代码并指导我进步。我已经用了一个星期了,我的头快要爆炸了!

我已尝试将 onMessage 函数中的所有代码包含在 content_script 中。我尝试翻转哪些 .js 文件正在发送消息,并尝试从扩展中发送输入字段作为响应。我试图将 sendMessage 排除在 .click DOM 函数之外。审查了 Google Chrome 扩展上的消息 API,我很困惑。

content.js 文件

chrome.runtime.onMessage.addListener (
    function(request) {
    item_name = request.name;
    item_color = request.color;
    item_category = request.category;
    item_size = request.size;

    console.log(item_name);
    console.log(item_color);
    console.log(item_category);
    console.log(item_size);
});

var url = window.location.href;
var i;

var item_category;
var item_name;
var item_color;
var item_size;

console.log(item_name);
console.log(item_color);
console.log(item_category);
console.log(item_size);

popup.js 文件

document.getElementById("save_input").onclick = function() {
  var item_name_save = document.getElementById("item_name_save").value;
  var item_color_save = document.getElementById("item_color_save").value;
  var item_category_save = document.getElementById("item_category_save").value;
  var item_size_save = document.getElementById("item_size_save").value;

  chrome.storage.sync.set({'item_name_save' : item_name_save}, function() {
    console.log('Value is set to ' + item_name_save);
  });
  chrome.storage.sync.set({'item_color_save' : item_color_save}, function() {
    console.log('Value is set to ' + item_color_save);
  });
  chrome.storage.sync.set({'item_category_save' : item_category_save}, function() {
    console.log('Value is set to ' + item_category_save);
  });
  chrome.storage.sync.set({'item_size_save' : item_size_save}, function() {
    console.log('Value is set to ' + item_size_save);
  });

  const msg = { name: item_name_save,
    color: item_color_save,
    category: item_category_save,
    size: item_size_save
  };
  chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
    chrome.tabs.sendMessage(tabs[0].id, msg, function() {
    });
  });

  location.reload();
  // window.close();
}

manifest.json

{
    "manifest_version": 2,

    "name": "Supreme bot",
    "description": "A bot for Supreme",
    "version": "1.0",

    "background": {
      "scripts": ["background.js"]
    },
    "browser_action": {
      "default_icon": "icon.png",
      "default_popup": "popup.html",
      "default_title": "Supreme bot"
    },
    "content_scripts": [
        {
            "matches": ["http://*/*", "https://*/*"],
            "js": ["bot-functionallity.js"]
        }
    ],
    "permissions": [
      "activeTab",
      "tabs",
      "storage",
      "tabs"
    ]
}

(当所有代码都在 onMessage 函数中时,可以正常工作但不符合预期) The Incorrect Functionality

我基本上希望 content.js 不能全部包含在 onMessage 函数中,因为它不能正常工作,并让它在 onMessage 函数的全局范围内分配值。我只想将该信息从 popup.js 发送到 content.js 1 次。

【问题讨论】:

  • onMessage 只是注册一个回调,该回调后续代码运行之后调用,因此全局公开值没有意义。这就是 JavaScript 事件循环的工作原理。
  • @wOxxOm 那么如何从 popup.js 中获取输入字段并在 content.js 中全局使用它们呢?我发布的链接显示了该功能的用途,否则我必须在运行扩展之前对这些值进行硬编码。 chrome 消息是我能找到的将输入/数据从扩展的 1 个文件发送到另一个文件的唯一方法。
  • 要么 1) 将所有内容放入 onMessage 并修复代码以使其正常运行,要么 2) 在 manifest.json 中使用 chrome.tabs.executeScript 而不是 content_scripts 两次:第一次使用 code 参数设置全局值,第二个使用file 运行内容脚本。
  • @wOxxOm 我已经尝试将所有内容都放入 onMessage 一周,所以我想我会查看 chrome.tabs.executeScript
  • @wOxxOm 好吧,我让它“工作”了 .addListener 中的所有内容......但现在它每次打开 popup.html 时都会监听以运行部分代码!我该怎么办

标签: javascript google-chrome google-chrome-extension


【解决方案1】:

我只是用chrome.storage.sync.set 设置值并在另一个文件中使用chrome.storage.sync.get...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 2014-04-11
    • 1970-01-01
    • 2015-06-01
    • 2013-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多