0

我希望这不是含糊......无法通过鼠标从content_script坐标弹出(Chrome扩展)

我的工作我的第一个谷歌浏览器扩展程序,有了它我试图转换this script看下面我做了一个扩展弹出。这个想法是,在右下角的那个页面上出现的框会出现在扩展的弹出框中,而动态地(实时地)从实际页面拉出鼠标坐标。我想这样做的方法是注入一个content_script,它将拉动鼠标坐标 - >将这些内容发送到background.html - >然后将这些传递给popup.js

我仔细研究了Google的文档,已经遵循了解决这个问题的几个帖子的建议,但我似乎无法得到这个工作。我认为或许我在计算出chrome.extension.sendRequest时遇到问题,有没有人做过这样的事情?你有例子吗?我是否以这种错误的方式去做?

// UPDATE:

(注:这是不工作)

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(); 
+0

您发送了da从“内容脚本”直接到“弹出”。 – 2012-04-10 04:09:25

+0

您的基本代码无效('loadDiv'未定义),甚至没有Chrome扩展。你能显示你的*当前*尝试的代码吗? – 2012-04-10 11:23:28

+0

嘿罗布,刚刚更新了原来的脚本(在帖子中丢失了一些代码)以及发布我的扩展src(纳入德里克的建议)。日Thnx! – Nick 2012-04-10 15:49:12

回答

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 
    } 
    }); 
+1

1.通道上的消息是JSON序列化的,所以你不必使用'JSON。*'方法。 2.即使你使用它,也不要在同一个字符串上使用'JSON.parse'两次。相反,将解析的JSON字符串保存在一个变量中。 – 2012-04-10 11:20:57

+0

@RobW - Woot!永远不会知道! – 2012-04-10 23:19:16