0

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

我正在开发我的第一个 google chrome 扩展程序,我正在尝试将此脚本转换为下面我制作的扩展程序弹出窗口。这个想法是出现在该页面右下角的框将出现在扩展的弹出窗口中,同时动态(实时)从实际页面中拉出鼠标坐标。我想这样做的方法是注入一个 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();
4

1 回答 1

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
    }
  });
于 2012-04-10T04:16:51.750 回答