javascript - get mouse coordinate in google chrome extension - Stack Overflow

I desperately looking for the method to retrieve the mouse coordinates of the user when a click on my c

I desperately looking for the method to retrieve the mouse coordinates of the user when a click on my contextMenu or when using a shortcutKey

I would like if possible not to have to use the onmousemove event that requires movement of the user :/

Do you know how?

Thank you in advance for your response

I desperately looking for the method to retrieve the mouse coordinates of the user when a click on my contextMenu or when using a shortcutKey

I would like if possible not to have to use the onmousemove event that requires movement of the user :/

Do you know how?

Thank you in advance for your response

Share Improve this question edited Mar 31, 2014 at 8:35 oktapodia asked Mar 9, 2014 at 3:48 oktapodiaoktapodia 1,7681 gold badge15 silver badges32 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

This is just a simple sample, and only works with:


files -> change "matches": ["file:"] in manifest.json to add new capabilities

context menu selection -> change contexts: ["selection"] in contextMenus.create (bg.js) to add new capabilities

secondary mouse button
-> change (mousePos.button == 2) in (c.js) to add new capabilities

You can try also with mousedown event

For run and test, create these three files, load the extension in chrome, load any file in chrome (example.txt), select any text and then, (secondary mouse button click) new context menu appears. Just click for get Cursor Position.

Tested and working: 26 march 2014 on chrome Versión 33.0.1750.154

Any ments are wele ;)

manifest.json

{
  "name": "menuContext position",
  "version": "0.1",
  "description": "determine menuContext position",
  "permissions": ["contextMenus"],
  "content_security_policy": "script-src 'self'; object-src 'self'",
  "background": {
    "scripts": ["bg.js"]
  },
  "content_scripts": [{
    "matches": ["file:///*/*"],
    "js": ["c.js"],
    "run_at": "document_end",
    "all_frames": true
  }],
  "manifest_version": 2
}

c.js

'use strict';

// when mouse up, send message to background.js with this position
document.addEventListener('mouseup', function (mousePos) {
  if (mousePos.button == 2) {
    var p = {clientX: mousePos.clientX, clientY: mousePos.clientY};
    var msg = {text: 'example', point: p, from: 'mouseup'};
    chrome.runtime.sendMessage(msg, function(response) {});
  }
})

bg.js

'use strict';

//global var for store cursor position
var gPos = null;

//receiving message
chrome.extension.onMessage.addListener(function(msg, sender, sendResponse) {
  if (msg.from == 'mouseup') {
    //storing position
    gPos = msg.point;
  }
})

// onclick callback function.
function OnClick(info, tab, text, mousePos) {
  if (info.menuItemId == idConsole) {
      if (gPos != null) {
          alert('Position X: ' + gPos.clientX + '\nPosition Y: ' + gPos.clientY );
          //console.log('Position X: ' + gPos.clientX + '\nPosition Y: ' + gPos.clientY );
      }
  }
}

//on click sample callback with more params
var idConsole = chrome.contextMenus.create({
  title: 'Cursor Position',
  contexts: ["selection"],
  onclick: function(info, tab) {
    OnClick(info, tab, '%s', gPos);
  }
})

Please, if possible, add tag [google-chrome-extension] to your question. Greetings

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744401165a4572394.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信