Adds customizable user script and user stylesheet.

This commit is contained in:
James Magahern
2021-12-16 18:40:45 -08:00
parent 026306b6df
commit b79f0ac5db
7 changed files with 143 additions and 2 deletions

View File

@@ -45,6 +45,8 @@ NS_SWIFT_NAME(ProcessBundleBridge)
- (void)parseDocumentForReaderMode:(void(^)(NSString *))completionBlock NS_SWIFT_NAME(parseDocumentForReaderMode(completion:));
- (void)reloadCustomizedUserScriptsAndStylesheets;
- (instancetype)init NS_UNAVAILABLE;
@end

View File

@@ -34,6 +34,10 @@
WKUserScript *_readabilityScript;
NSArray<WKUserScript *> *_userScripts;
// These come from settings.
_WKUserStyleSheet *_customizedUserStylesheet;
WKUserScript *_customizedUserScript;
}
- (void)tearDown
@@ -101,6 +105,9 @@
[userContentController addUserScript:script];
}
// Reload customized user scripts/stylesheets from settings
[self reloadCustomizedUserScriptsAndStylesheets];
// Instantiate web view
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:webViewConfiguration];
@@ -135,6 +142,30 @@
return _userScripts;
}
- (void)reloadCustomizedUserScriptsAndStylesheets
{
WKUserContentController *userContentController = [_webViewConfiguration userContentController];
if (_customizedUserScript) {
[userContentController _removeUserScript:_customizedUserScript];
}
NSString *scriptSource = [[NSUserDefaults standardUserDefaults] stringForKey:@"userScript"];
if ([scriptSource length]) {
_customizedUserScript = [[WKUserScript alloc] initWithSource:scriptSource injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
[userContentController addUserScript:_customizedUserScript];
}
if (_customizedUserStylesheet) {
[userContentController _removeUserStyleSheet:_customizedUserStylesheet];
}
NSString *stylesheetSource = [[NSUserDefaults standardUserDefaults] stringForKey:@"userStylesheet"];
if ([stylesheetSource length]) {
_customizedUserStylesheet = [[_WKUserStyleSheet alloc] initWithSource:stylesheetSource forMainFrameOnly:YES];
[userContentController _addUserStyleSheet:_customizedUserStylesheet];
}
}
#pragma mark <SBRWebProcessDelegate>
- (void)webProcessDidConnect