diff --git a/README.md b/README.md index 81f6f2d..1c92a99 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ Learn more: [electronjs/doc](https://www.electronjs.org/docs/latest/api/extensio | [@noi/ask](https://github.com/lencx/Noi/tree/main/extensions/noi-ask) | 0.1.9 | The best assistant for batch asking and quick typing of prompts. | | [@noi/ask-custom](https://github.com/lencx/Noi/tree/main/extensions/noi-ask-custom) | 0.1.0 | The best assistant for batch asking and quick typing of prompts. | | [@noi/export-chatgpt](https://github.com/lencx/Noi/tree/main/extensions/noi-export-chatgpt) | 0.1.1 | ChatGPT chat history export, supports PDF, Image, and Markdown formats. | -| [@noi/reset](https://github.com/lencx/Noi/tree/main/extensions/noi-reset) | 0.1.1 | Reset certain website styles to enhance compatibility with Noi. | +| [@noi/reset](https://github.com/lencx/Noi/tree/main/extensions/noi-reset) | 0.1.3 | Reset certain website styles to enhance compatibility with Noi. | [![Star History Chart](https://api.star-history.com/svg?repos=lencx/Noi&type=Timeline)](https://star-history.com/#lencx/Noi&Timeline) diff --git a/extensions/README.md b/extensions/README.md index 7c9ff99..8c0d3db 100644 --- a/extensions/README.md +++ b/extensions/README.md @@ -12,5 +12,5 @@ Learn more: [electronjs/doc](https://www.electronjs.org/docs/latest/api/extensio | [@noi/ask](https://github.com/lencx/Noi/tree/main/extensions/noi-ask) | 0.1.9 | The best assistant for batch asking and quick typing of prompts. | | [@noi/ask-custom](https://github.com/lencx/Noi/tree/main/extensions/noi-ask-custom) | 0.1.0 | The best assistant for batch asking and quick typing of prompts. | | [@noi/export-chatgpt](https://github.com/lencx/Noi/tree/main/extensions/noi-export-chatgpt) | 0.1.1 | ChatGPT chat history export, supports PDF, Image, and Markdown formats. | -| [@noi/reset](https://github.com/lencx/Noi/tree/main/extensions/noi-reset) | 0.1.2 | Reset certain website styles to enhance compatibility with Noi. | +| [@noi/reset](https://github.com/lencx/Noi/tree/main/extensions/noi-reset) | 0.1.3 | Reset certain website styles to enhance compatibility with Noi. | \ No newline at end of file diff --git a/extensions/noi-reset/base.js b/extensions/noi-reset/base.js new file mode 100644 index 0000000..5a3daed --- /dev/null +++ b/extensions/noi-reset/base.js @@ -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); diff --git a/extensions/noi-reset/darg.js b/extensions/noi-reset/darg.js index a7ed7a6..0b2f98d 100644 --- a/extensions/noi-reset/darg.js +++ b/extensions/noi-reset/darg.js @@ -11,4 +11,5 @@ function removeAppRegion() { } } -setTimeout(removeAppRegion, 3000); +setTimeout(removeAppRegion, 1000); +window.NoiUtils?.changeURL?.('noi@reset:drag', () => setTimeout(removeAppRegion, 1000)); diff --git a/extensions/noi-reset/manifest.json b/extensions/noi-reset/manifest.json index 58d2504..082225c 100644 --- a/extensions/noi-reset/manifest.json +++ b/extensions/noi-reset/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 3, "name": "@noi/reset", - "version": "0.1.2", + "version": "0.1.3", "homepage": "https://github.com/lencx/Noi/tree/main/extensions/noi-reset", "description": "Reset certain website styles to enhance compatibility with Noi.", "content_scripts": [ @@ -9,7 +9,7 @@ "matches": [ "" ], - "js": ["scrollbar.js", "darg.js"], + "js": ["base.js", "scrollbar.js", "darg.js"], "run_at": "document_end", "world": "MAIN" }