【问题标题】:can't pass mouse coordinates from content_script to popup (chrome extension)无法将鼠标坐标从 content_script 传递到弹出窗口(chrome 扩展)
【发布时间】:2012-04-22 09:31:41
【问题描述】:

我希望这不是含糊的......

我正在开发我的第一个 google chrome 扩展程序,我正在尝试将this script见下文转换为扩展程序弹出窗口。这个想法是,出现在该页面右下角的框将出现在扩展的弹出窗口中,同时动态(实时)从实际页面中拉出鼠标坐标。我想这样做的方法是注入一个 content_script 来拉动鼠标坐标 -> 将它们发送到 background.html -> 将它们传递给 popup.js

我仔细研究了 google 的文档,并遵循了解决此问题的几篇帖子的建议,但我似乎无法让它发挥作用。我想也许我在弄清楚chrome.extension.sendRequest 时遇到了问题,以前有没有人做过这样的事情?你有例子吗?我是不是走错了路?

//更新:

(注意:这不起作用)

manifest.json
====================
"browser_action": {
  "default_icon": "icon.png",
  "default_popup": "popup.html"
},
"content_scripts": [
  {
    "matches": ["<all_urls>","http://*/*","https://*/*"],
    "js": ["coord.js"]
  }
]


content_script (i.e. coord.js)
====================
var x = event.clientX,
    y = event.clientY;  //record down the x and y

chrome.extension.onRequest.addListener(         //listen to request
  function(request, sender, sendResponse) {
    if (request.greeting == "coord"){
      sendResponse({farewell: JSON.stringify([x,y])});//send coordinates to poupup
    }
  });


popup.js
====================
    chrome.tabs.getSelected(null, function(tab) {    //ask for coordinates
      chrome.tabs.sendRequest(tab.id, {greeting: "coord"}, function(response) {
        var x = JSON.parse(response.farewell)[0],
            y = JSON.parse(response.farewell)[1];

        document.getElementById("main").innerHTML = x + "," + y;
      });
    });

再次,我正在尝试修改我编写的这个脚本:

    var width, height, divObj, interval;
    var l, t, r, b;

    function setup() {
            width = window.innerWidth;
            height = window.innerHeight;
            interval = setInterval(loadDiv, 50);
    }

    document.onmousemove=getMouseCoordinates;

    function getMouseCoordinates(event) {
        ev = event || window.event;

        l = ev.pageX; t = ev.pageY;
        r = width - l; b = height - t;

        divObj.innerHTML = '<div style="position: absolute; left: 20px;">.class {<br>&nbsp;&nbsp;&nbsp;position: absolute;<br>&nbsp;&nbsp;&nbsp;left: ' + l + 'px;<br>&nbsp;&nbsp;&nbsp;top: ' + t + 'px;<br>}</div><div style="position: absolute; left: 250px;">.class {<br>&nbsp;&nbsp;&nbsp;position: absolute;<br>&nbsp;&nbsp;&nbsp;right: ' + r + 'px;<br>&nbsp;&nbsp;&nbsp;bottom: ' + b + 'px;<br>}</div>';      
    }

    function loadDiv() {
        divObj = document.getElementById("divPlacement");
    }

    document.write('<div id="divPlacement" style="position: absolute; right: 25px; bottom: 25px; z-index: 1000; color: #fff; font-family: monospace; background-color: #000; opacity:0.4; filter:alpha(opacity=40); -webkit-border-radius: 5px;-moz-border-radius: 5px; border-radius: 5px; padding: 10px; width: 420px; height: 80px; border: solid #ccc;"></div>');

    setup();

【问题讨论】:

  • 您将数据直接从content script 发送到popup
  • 您的基本代码无效(loadDiv 未定义),甚至不是 Chrome 扩展程序。你能显示你当前尝试的代码吗?
  • 嘿,Rob,刚刚更新了原始脚本(帖子中缺少一些代码)以及目前发布我的扩展 src(包含 Derek 的建议)。谢谢!

标签: javascript google-chrome-extension message-passing mouse-coordinates


【解决方案1】:

阅读更多:http://code.google.com/chrome/extensions/messaging.html#simple

popup.html
===============
chrome.tabs.getSelected(null, function(tab) {    //ask for coordinates
  chrome.tabs.sendRequest(tab.id, {greeting: "coord"}, function(response) {
    var x = JSON.parse(response.farewell)[0],
        y = JSON.parse(response.farewell)[1];
    console.log(x);  //Will give you mouse x
    console.log(y);  //Will give you mouse y
  });
});

content script
===============
var x = event.clientX,
    y = event.clientY;  //record down the x and y

chrome.extension.onRequest.addListener(         //listen to request
  function(request, sender, sendResponse) {
    if (request.greeting == "coord"){
      sendResponse({farewell: JSON.stringify([x,y]));//send coordinates to poupup
    }
  });

【讨论】:

  • 1.通道上的消息是 JSON 序列化的,因此您不必使用 JSON.* 方法。 2.即使你使用它,也不要在同一个字符串上使用两次JSON.parse。相反,将解析后的 JSON 字符串保存在变量中。
  • @RobW - 哇!永远不知道!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-31
  • 1970-01-01
  • 1970-01-01
  • 2011-09-27
  • 2016-11-13
相关资源
最近更新 更多