mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-09 01:50:30 +00:00
feat(extension): expose host version and WebView page events
This commit is contained in:
1 parent
7c1447f659
commit
dfe8e9a3fd
27 files changed
+1230
-191
No files matched your search
@@ -0,0 +1,40 @@
|
||||
import 'dart:convert';
|
||||
|
||||
/// Runs before page scripts and reports only the main document's events.
|
||||
String buildWebViewEventScript(String channel) =>
|
||||
'''
|
||||
(() => {
|
||||
if (window !== window.top) return;
|
||||
const pending = [];
|
||||
const flush = () => {
|
||||
const bridge = window.flutter_inappwebview;
|
||||
if (!bridge || typeof bridge.callHandler !== 'function') return;
|
||||
while (pending.length) bridge.callHandler(${jsonEncode(channel)}, JSON.stringify(pending.shift()));
|
||||
};
|
||||
const send = (event, data) => { pending.push({event, data}); flush(); };
|
||||
window.addEventListener('flutterInAppWebViewPlatformReady', flush);
|
||||
let lastURL = String(location.href);
|
||||
const changed = () => {
|
||||
const url = String(location.href);
|
||||
if (url === lastURL) return;
|
||||
lastURL = url;
|
||||
send('url-changed', {url, sameDocument: true});
|
||||
};
|
||||
send('url-changed', {url: lastURL, sameDocument: false});
|
||||
for (const name of ['pushState', 'replaceState']) {
|
||||
const original = history[name];
|
||||
history[name] = function(...args) {
|
||||
const result = Reflect.apply(original, this, args);
|
||||
changed();
|
||||
return result;
|
||||
};
|
||||
}
|
||||
window.addEventListener('hashchange', changed);
|
||||
window.addEventListener('popstate', changed);
|
||||
window.addEventListener('pageshow', (event) => {
|
||||
if (event.persisted) send('url-changed', {url: String(location.href), sameDocument: false});
|
||||
else changed();
|
||||
});
|
||||
window.addEventListener('load', () => send('load', {url: String(location.href)}), {once: true});
|
||||
})();
|
||||
''';
|
||||
Reference in new issue
Block a user