Tôi đang đăng câu hỏi này ở đây, vì tôi không thể đăng câu hỏi trên diễn đàn mở rộng chính thức của Chromium (hoặc có sự chậm trễ tuyệt vời cho đến khi nó được kiểm duyệt). Tôi phải kiểm tra tiện ích mở rộng của Chromium cho dù có người nghe loại sự kiện cụ thể được đính kèm với phần tử HTML tùy ý hay không. Trong Firefox, tôi có thể sử dụng dịch vụ sau để nhận thông tin này:Cách tìm hiểu loại trình nghe sự kiện nào được gắn với phần tử HTML cụ thể trong tiện ích mở rộng của Chrome?
var listenerService = Components.classes["@mozilla.org/eventlistenerservice;1"]
.getService(Components.interfaces.nsIEventListenerService);
var infos = listenerService.getListenerInfoFor(element, {});
var types = [];
for (var i = 0; i < infos.length; ++i) {
var info = infos[i].QueryInterface(Components.interfaces.nsIEventListenerInfo);
types.push(info.type);
}
Như tôi thấy trong Chromium không có API tương tự. Vì vậy, tôi đã thử các kỹ thuật sau đây (được đề nghị here):
tôi đã tạo ra kịch bản events_spy.js
:
(function(original) {
Element.prototype.addEventListener = function(type, listener, useCapture) {
if (typeof (this._handlerTypes) == 'undefined') {
this._handlerTypes = {};
}
this._handlerTypes[type] = true;
return original.apply(this, arguments);
}
})(Element.prototype.addEventListener);
(function(original) {
Element.prototype.removeEventListener = function(type, listener,useCapture) {
if (typeof (this._handlerTypes) != 'undefined') {
delete this._handlerTypes[type];
}
return original.apply(this, arguments);
}
})(Element.prototype.removeEventListener);
Tôi tuyên bố kịch bản này trong manifest.json
như sau:
"content_scripts" : [{
"matches" : [ "http://*/*", "https://*/*" ],
"js" : [ "content/events_spy.js" ],
"run_at" : "document_start",
"all_frames" : true
},
...
]
Sau đó, tôi thử nghiệm tiện ích mở rộng của mình trên trang HTML sau:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<a id="test" href="#">Click here</a>
<script type="application/javascript">
document.getElementById("test").addEventListener("click", function()
{ alert("clicked"); }, false);
</script>
</body>
</html>
Thật không may, tính năng này không hoạt động - Tôi không thể thấy trình gỡ lỗi dừng lại bên trong chức năng tùy chỉnh addEventListener()
của tôi. Tôi đang làm gì sai?
Cảm ơn!
EDIT: Final (bẩn) giải pháp, nhờ @kdzwinel
var injectedJS = "\
(function(original) { \
Element.prototype.addEventListener = function(type, listener, useCapture) { \
var attr = this.getAttribute('_handlerTypes'); \
var types = attr ? attr.split(',') : []; \
var found = false; \
for (var i = 0; i < types.length; ++i) { \
if (types[i] == type) { \
found = true; \
break; \
} \
} \
if (!found) { \
types.push(type); \
} \
this.setAttribute('_handlerTypes', types.join(',')); \
return original.apply(this, arguments); \
} \
})(Element.prototype.addEventListener); \
\
(function(original) { \
Element.prototype.removeEventListener = function(type, listener, useCapture) { \
var attr = this.getAttribute('_handlerTypes'); \
var types = attr ? attr.split(',') : []; \
var removed = false; \
for (var i = 0; i < types.length; ++i) { \
if (types[i] == type) { \
types.splice(i, 1); \
removed = true; \
break; \
} \
} \
if (removed) { \
this.setAttribute('_handlerTypes', types.join(',')); \
} \
return original.apply(this, arguments); \
} \
})(Element.prototype.removeEventListener); \
";
var script = document.createElement("script");
script.type = "text/javascript";
script.appendChild(document.createTextNode(injectedJS));
document.documentElement.appendChild(script);
yếu tố
Mỗi HTML mà có nghe sự kiện đính kèm sẽ có một thuộc tính đặc biệt "_handlerTypes", trong đó có chứa dấu phẩy tách ra danh sách các sự kiện . Và thuộc tính này có thể truy cập được từ tập lệnh nội dung của tiện ích mở rộng của Chrome!
Cảm ơn rất nhiều cho việc cập nhật với giải pháp của bạn! –