mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-17 06:48:37 +02:00
improve link detection in notify-link-clicks-i18n
Original approach is a bit too naive and misses all links that contain other elements which can grab mouse clicks. E. g. simple target checking fails on the grid @ mozilla.org. Traversing up the tree to look for valid links fixes this problem.
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
window.addEventListener("click", notifyExtension);
|
||||
|
||||
function notifyExtension(e) {
|
||||
console.log("content script sending message");
|
||||
if (e.target.tagName != "A") {
|
||||
return;
|
||||
var target = e.target;
|
||||
while ((target.tagName != "A" || !target.href) && target.parentNode) {
|
||||
target = target.parentNode;
|
||||
}
|
||||
chrome.runtime.sendMessage({"url": e.target.href});
|
||||
if (target.tagName != "A")
|
||||
return;
|
||||
|
||||
console.log("content script sending message");
|
||||
chrome.runtime.sendMessage({"url": target.href});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user