mirror of
https://github.com/chylex/TweetDuck.git
synced 2025-05-27 23:34:07 +02:00
Refactor and optimize event handling in notification.js
This commit is contained in:
parent
af7657e3f8
commit
4c44da7f4a
@ -1,44 +1,30 @@
|
|||||||
(function($TD){
|
(function($TD){
|
||||||
//
|
//
|
||||||
// Function: Bubbles up the parents until it hits an element with the specified tag (includes the first element), and returns true if the search was successful.
|
// Variable: Collection of all <a> tags.
|
||||||
//
|
//
|
||||||
var bubbleParents = function(element, tag, callback){
|
var links = document.getElementsByTagName("A");
|
||||||
do{
|
|
||||||
if (element.tagName === "A"){
|
|
||||||
callback(element);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}while((element = element.parentElement) != null);
|
|
||||||
|
|
||||||
return false;
|
|
||||||
};
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Function: Adds an event listener which calls listener(event, element) when an event was triggered by an element of the specified type or one of its children.
|
// Function: Adds an event listener to all elements in the array or collection.
|
||||||
//
|
//
|
||||||
EventTarget.prototype.addBubbledEventListener = function(element, type, listener){
|
var addEventListener = function(collection, type, listener){
|
||||||
this.addEventListener(type,function(e){
|
for(let index = 0; index < collection.length; index++){
|
||||||
bubbleParents(e.target,element.toUpperCase(),function(ele){
|
collection[index].addEventListener(type,listener);
|
||||||
listener(e,ele);
|
}
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
//
|
//
|
||||||
// Block: Hook into links to bypass default open function.
|
// Block: Hook into links to bypass default open function.
|
||||||
//
|
//
|
||||||
document.body.addEventListener("click",function(e){
|
addEventListener(links,"click",function(e){
|
||||||
if (bubbleParents(e.target,"A",function(ele){
|
$TD.openBrowser(e.currentTarget.getAttribute("href"));
|
||||||
$TD.openBrowser(ele.getAttribute("href"));
|
e.preventDefault();
|
||||||
})){
|
|
||||||
e.preventDefault();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//
|
//
|
||||||
// Block: Allow bypassing of t.co in context menus.
|
// Block: Allow bypassing of t.co in context menus.
|
||||||
//
|
//
|
||||||
document.body.addBubbledEventListener("a","contextmenu",function(e, ele){
|
addEventListener(links,"contextmenu",function(e){
|
||||||
$TD.setLastRightClickedLink(ele.getAttribute("data-full-url") || "");
|
$TD.setLastRightClickedLink(e.currentTarget.getAttribute("data-full-url") || "");
|
||||||
});
|
});
|
||||||
})($TD);
|
})($TD);
|
Loading…
Reference in New Issue
Block a user