无法将content_script中的鼠标坐标传递给弹出窗口(chrome扩展名)

时间:2012-04-10 03:48:14

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

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

我正在开发我的第一个google chrome扩展程序,我正在尝试转换this script 请参阅下面的我做了一个扩展弹出窗口。这个想法是在右下角的页面上显示的框将显示在扩展的弹出窗口中,而动态(实时)则从实际页面中拉出鼠标坐标。我认为这样做的方法是注入一个拉动鼠标坐标的content_script - >将这些发送到background.html - >然后将那些传递给popup.js

我已经仔细阅读了谷歌的文档,我已经按照了解决这个问题的几篇文章的建议,但我似乎无法让这个工作。我想也许我在弄清楚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();

1 个答案:

答案 0 :(得分:0)

了解详情: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
    }
  });