idealista-enhancer/src/Event.js

23 lines
678 B
JavaScript
Raw Normal View History

import Log from "./Log.js";
import StringUtil from "./StringUtil.js";
export default class Event {
static click(selector, callback) {
this._addEvent('click', selector, callback)
}
static change(selector, callback) {
this._addEvent('change', selector, callback)
}
static _addEvent(eventName, selector, callback) {
Log.debug(`Adding event: ${eventName} to: ${selector}`);
document.addEventListener(eventName, (event) => {
if (!event.target.matches(selector)) return;
Log.debug(`${StringUtil.capitalizeFirstLetter(eventName)} on: ${selector}`);
callback(event.target);
});
}
}