2015-02-12 58 views
1

在我的分機我想隱藏/基於URL中的上下文菜單上顯示的項目,我得到:Firefox擴展:獲取點擊數據時上下文菜單

  1. 從一個鏈接,如果用戶在打開上下文菜單一個,
  2. 從選定的文本,也如果用戶在文本上打開上下文菜單。

在功能顯示/隱藏的項目在上下文菜單中我做以下檢查:

if (gContextMenu.onLink) { 
    url = gContextMenu.target.href; 
}  
if (gContextMenu.isTextSelected) { 
    url = content.window.getSelection(); 
}   

如果一些文字在網頁中選擇,用戶通過鏈接打開上下文菜單,這兩個條件都是真實的。此外,如果選擇了某些文本,並且用戶在頁面的任何位置打開了上下文菜單(在選擇或不選中),isTextSelected標誌也是如此。

有沒有一種方法可以檢測到用戶使用右鍵點擊的真正元素是什麼?我怎麼知道右鍵是否在選定的文字上?

回答

0

首先,有gContextMenu.target,其中包含光標下的Node(如果有的話)(也可能是null,非常少見)。

gContextMenu代碼實際上uses gContextMenu.target to initialize things like .onLink

至於檢查是否聚焦節點的當前選擇中實際上包含,你可以在文檔集中使用.getSelection().containsNode()

var isFocusedNodeInCurrentSelection = false; 
var selection = gContextMenu.focusedWindow && 
    gContextMenu.focusedWindow.getSelection(); 
if (selection) { 
    isFocusedNodeInCurrentSelection = 
    selection.containsNode(gContextMenu.target, true); 
} 
// Do something with that information.