Expose installed version string to update notes HTML page (#2373)

This commit is contained in:
Nathan Manceaux-Panot authored and GitHub committed 2023-07-11 22:07:42 -07:00
1 parent 5528d7f9ec
commit 7c8fe1d427
3 files changed
+37 -4

No files matched your search

+1 -1
View File
@@ -356,7 +356,7 @@ static NSString *const SUUpdateAlertTouchBarIndentifier = @"" SPARKLE_BUNDLE_IDE
} else
#endif
{
_releaseNotesView = [[SUWKWebView alloc] initWithColorStyleSheetLocation:colorStyleURL fontFamily:defaultFontFamily fontPointSize:defaultFontSize javaScriptEnabled:javaScriptEnabled customAllowedURLSchemes:customAllowedURLSchemes];
_releaseNotesView = [[SUWKWebView alloc] initWithColorStyleSheetLocation:colorStyleURL fontFamily:defaultFontFamily fontPointSize:defaultFontSize javaScriptEnabled:javaScriptEnabled customAllowedURLSchemes:customAllowedURLSchemes installedVersion: _host.version];
}
}
+1 -1
View File
@@ -15,7 +15,7 @@ NS_ASSUME_NONNULL_BEGIN
SPU_OBJC_DIRECT_MEMBERS @interface SUWKWebView : NSObject <SUReleaseNotesView>
- (instancetype)initWithColorStyleSheetLocation:(NSURL *)colorStyleSheetLocation fontFamily:(NSString *)fontFamily fontPointSize:(int)fontPointSize javaScriptEnabled:(BOOL)javaScriptEnabled customAllowedURLSchemes:(NSArray<NSString *> *)customAllowedURLSchemes;
- (instancetype)initWithColorStyleSheetLocation:(NSURL *)colorStyleSheetLocation fontFamily:(NSString *)fontFamily fontPointSize:(int)fontPointSize javaScriptEnabled:(BOOL)javaScriptEnabled customAllowedURLSchemes:(NSArray<NSString *> *)customAllowedURLSchemes installedVersion:(NSString *)installedVersion;
@end
+35 -2
View File
@@ -53,7 +53,27 @@ static WKUserScript *_userScriptWithInjectedStyleSource(NSString *styleSource)
return [[WKUserScript alloc] initWithSource:scriptSource injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
}
- (instancetype)initWithColorStyleSheetLocation:(NSURL *)colorStyleSheetLocation fontFamily:(NSString *)fontFamily fontPointSize:(int)fontPointSize javaScriptEnabled:(BOOL)javaScriptEnabled customAllowedURLSchemes:(NSArray<NSString *> *)customAllowedURLSchemes
static WKUserScript *_userScriptForExposingCurrentRelease(NSString *releaseString)
{
// Check that release string can be safely injected
NSMutableCharacterSet *allowedCharacterSet = [NSMutableCharacterSet alphanumericCharacterSet];
[allowedCharacterSet addCharactersInString:@"_.- "];
if ([releaseString rangeOfCharacterFromSet:allowedCharacterSet.invertedSet].location != NSNotFound) {
SULog(SULogLevelDefault, @"warning: App version '%@' has characters unsafe for injection. The version number will not be exposed to the release notes CSS. Only [a-zA-Z0-9._- ] is allowed.", releaseString);
return nil;
}
// This script adds the `sparkle-installed-version` class to all elements which have a matching `data-sparkle-version` attribute
NSString *scriptSource = [NSString stringWithFormat:
@"document.querySelectorAll(\'[data-sparkle-version=\"%@\"]\')\n"
@".forEach(installedVersionElement =>\n"
@"installedVersionElement.classList.add('sparkle-installed-version')\n"
@");", releaseString];
return [[WKUserScript alloc] initWithSource:scriptSource injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
}
- (instancetype)initWithColorStyleSheetLocation:(NSURL *)colorStyleSheetLocation fontFamily:(NSString *)fontFamily fontPointSize:(int)fontPointSize javaScriptEnabled:(BOOL)javaScriptEnabled customAllowedURLSchemes:(NSArray<NSString *> *)customAllowedURLSchemes installedVersion:(NSString *)installedVersion
{
self = [super init];
if (self != nil) {
@@ -92,7 +112,20 @@ static WKUserScript *_userScriptWithInjectedStyleSource(NSString *styleSource)
// In fact, we must execute javascript to properly inject our default CSS style into the DOM
// Legacy WebView has exposed methods for custom stylesheets and default fonts,
// but WKWebView seems to forgo that type of API surface in favor of user scripts like this
[userContentController addUserScript:_userScriptWithInjectedStyleSource(finalStyleContents)];
WKUserScript *userScriptWithInjectedStyleSource = _userScriptWithInjectedStyleSource(finalStyleContents);
if (userScriptWithInjectedStyleSource == nil) {
SULog(SULogLevelError, @"Failed to create script for injecting style");
} else {
[userContentController addUserScript:userScriptWithInjectedStyleSource];
}
WKUserScript *userScriptForExposingCurrentRelease = _userScriptForExposingCurrentRelease(installedVersion);
if (userScriptForExposingCurrentRelease == nil) {
SULog(SULogLevelDefault, @"warning: Failed to create script for injecting version %@", installedVersion);
} else {
[userContentController addUserScript:userScriptForExposingCurrentRelease];
}
configuration.userContentController = userContentController;
_webView = [[WKWebView alloc] initWithFrame:NSZeroRect configuration:configuration];