-
Notifications
You must be signed in to change notification settings - Fork 484
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
58 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
(function(history) { | ||
// Initialize storage for event listeners | ||
const eventListeners = { | ||
pushstate: {}, | ||
replacestate: {}, | ||
popstate: {} | ||
}; | ||
|
||
// Function to trigger all listeners for a specific event | ||
function triggerEventListeners(eventName, event) { | ||
Object.values(eventListeners[eventName]).forEach(listener => listener(event)); | ||
} | ||
|
||
// Function to override history methods and add our own logic | ||
function overrideHistoryMethod(methodName, eventName) { | ||
const originalMethod = history[methodName]; | ||
history[methodName] = function(state, ...rest) { | ||
const result = originalMethod.apply(this, [state, ...rest]); | ||
// Construct event object | ||
const event = { state: state, url: window.location.href }; | ||
triggerEventListeners(eventName, event); | ||
return result; | ||
}; | ||
} | ||
|
||
// Override both pushState and replaceState methods | ||
overrideHistoryMethod('pushState', 'pushstate'); | ||
overrideHistoryMethod('replaceState', 'replacestate'); | ||
|
||
// Listen to the native popstate event and trigger our listeners | ||
window.addEventListener('popstate', event => { | ||
triggerEventListeners('popstate', { url: window.location.href }); | ||
}); | ||
|
||
// Provide interface for registering and unregistering event listeners | ||
window.NoiUtils = { | ||
changeURL(id, callback) { | ||
if (typeof callback === 'function' && id) { | ||
// Avoid registering the same callback under the same ID | ||
eventListeners.pushstate[id] = callback; | ||
eventListeners.replacestate[id] = callback; | ||
eventListeners.popstate[id] = callback; | ||
} | ||
}, | ||
removeURL(id) { | ||
// Remove the listener by ID from all event types | ||
delete eventListeners.pushstate[id]; | ||
delete eventListeners.replacestate[id]; | ||
delete eventListeners.popstate[id]; | ||
} | ||
}; | ||
})(window.history); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters