Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ClassWatcher {
- constructor(targetNode, classToWatch, classAddedCallback, classRemovedCallback) {
- this.targetNode = targetNode
- this.classToWatch = classToWatch
- this.classAddedCallback = classAddedCallback
- this.classRemovedCallback = classRemovedCallback
- this.observer = null
- this.lastClassState = targetNode.classList.contains(this.classToWatch)
- this.init()
- }
- init() {
- this.observer = new MutationObserver(this.mutationCallback.bind(this))
- this.observe()
- }
- observe() {
- this.observer.observe(this.targetNode, { attributes: true })
- }
- disconnect() {
- this.observer.disconnect()
- }
- mutationCallback(mutationsList) {
- for (let mutation of mutationsList) {
- if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
- let currentClassState = mutation.target.classList.contains(this.classToWatch)
- if (this.lastClassState !== currentClassState) {
- this.lastClassState = currentClassState
- if (currentClassState) {
- this.classAddedCallback()
- } else {
- this.classRemovedCallback()
- }
- }
- }
- }
- }
- }
- function getEventHandlerName(target)
- {
- return Object.keys(target).find(
- prop => prop.startsWith('__reactEventHandlers')
- )
- }
- const targetNode = $('.reaction-time-test')
- const callbackNull = () => {}
- const callbackReation = () => {
- const handlerName = getEventHandlerName(targetNode)
- if (targetNode[handlerName] && targetNode[handlerName].onMouseDown) {
- targetNode[handlerName].onMouseDown()
- console.log('Click!')
- } else {
- console.log('Click handler not found.')
- }
- }
- const classWatcher = new ClassWatcher($('.reaction-time-test'), 'view-go', callbackReation, callbackNull)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement