如何将 popup.js 文件中的单个字符串变量传递到 chrome 扩展 API 上的单独 javascript 文件?

阿尔韦托·奥尔特加-加西亚

到目前为止,这是我的代码:popup.js

let drawCanvas = document.getElementById('but1');

let theInputColor = document.getElementById("colorPicker").value;

drawCanvas.onclick = function(undefined){
  chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
    chrome.tabs.executeScript(tabs[0].id, { file: "canvasDrawing.js"}, function(){
      chrome.tabs.sendMessage(tabs[0].id, theInputColor);
       });
  });
 };

变量 theInputColor 是我想传递给我的 CanvasDrawing.js 文件作为 context.strokeStyle 的值的变量

画布绘图.js

    ....function drawLine(context, x1, y1, x2, y2) {
         context.beginPath();
         context.strokeStyle = theInputColor;
         context.lineWidth = 3;
         context.moveTo(x1, y1);
         context.lineTo(x2, y2);
         context.stroke();
         context.closePath();
          };

我需要 chrome.tabs.onMessage 响应吗?

阿尔韦托·奥尔特加-加西亚

感谢回复评论,我能够找出解决我的问题的方法。虽然这两种方法都有效,但我发现使用存储 API 来发送多条消息要好得多。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章