buildUserScriptSource method
String
buildUserScriptSource(
- WebViewUserScript userScript, {
- required bool platformHandlesMainFrameOnly,
inherited
Builds the source installed by a platform user-script implementation.
Registered scripts execute in a consistent private function scope. Scripts that
need to expose state to later page code must assign it explicitly through
globalThis. Set platformHandlesMainFrameOnly when the native API can
perform frame filtering without changing the script source.
Implementation
@protected
String buildUserScriptSource(
WebViewUserScript userScript, {
required bool platformHandlesMainFrameOnly,
}) {
final String scopedSource =
'''
(function() {
${userScript.source}
}).call(globalThis);
''';
final String globalThisCompatibility = '''
if (typeof globalThis === 'undefined') {
window.globalThis = window;
}
''';
if (userScript.forMainFrameOnly && !platformHandlesMainFrameOnly) {
return '''
(function() {
$globalThisCompatibility
if (globalThis.top !== globalThis) {
return;
}
$scopedSource
}).call(globalThis);
''';
}
return '''
$globalThisCompatibility
$scopedSource''';
}