Skip to content

Commit

Permalink
feat: make webView iframe font size follow user custom font size
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng committed Dec 20, 2023
1 parent f864d5b commit 5b4466a
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 39 deletions.
8 changes: 4 additions & 4 deletions Easydict/Feature/Configuration/EZConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ - (void)setup {
self.fontSizes = @[@(1), @(1.1), @(1.2), @(1.3), @(1.4)];
[[NSUserDefaults standardUserDefaults]registerDefaults:@{kTranslationControllerFontKey: self.fontSizes.firstObject}];

self.fontSizeRatio = [[NSUserDefaults standardUserDefaults]floatForKey:kTranslationControllerFontKey];
self.fontSizeRatio = [[NSUserDefaults standardUserDefaults] floatForKey:kTranslationControllerFontKey];
}

#pragma mark - getter
Expand Down Expand Up @@ -418,9 +418,9 @@ - (void)setClearInput:(BOOL)clearInput {
[self logSettings:@{@"clear_input" : @(clearInput)}];
}

- (void)setFontSizeRatio:(CGFloat)currentFontSizeRatio {
_fontSizeRatio = currentFontSizeRatio;
[NSUserDefaults mm_write:@(currentFontSizeRatio) forKey:kTranslationControllerFontKey];
- (void)setFontSizeRatio:(CGFloat)fontSizeRatio {
_fontSizeRatio = fontSizeRatio;
[NSUserDefaults mm_write:@(fontSizeRatio) forKey:kTranslationControllerFontKey];
}

#pragma mark - Window Frame
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,17 @@
iframe.style.height = totalHeight + "px";

// console.log(`${i}: frame totalHeight: ${totalHeight}`);


var scrollHeight = getScrollHeight();

if (window.webkit && window.webkit.messageHandlers) {
// 调用 objc 方法
window.webkit.messageHandlers.objcHandler.postMessage({
method: "noteToUpdateScrollHeight",
scrollHeight: scrollHeight,
});
}
}
}

Expand Down Expand Up @@ -372,8 +383,10 @@
bigWordTitleHeight +
allIframesHeight +
dictionaryTitleHeight * iframesHeight.length;
console.log(`iframesHeight: [${iframesHeight}]`);
console.log(`scrollHeight: ${scrollHeight}`);

// console.log(`iframesHeight: [${iframesHeight}]`);
// console.log(`scrollHeight: ${scrollHeight}`);

return scrollHeight;
}

Expand Down Expand Up @@ -424,6 +437,19 @@
iframe.style.borderColor = newBorderColor;
}
}

// Change all iframes body font size
function changeIframeBodyFontSize(fontSize) {
console.log("fontSize: " + fontSize);
var iframes = document.getElementsByTagName('iframe');
for (var i = 0; i < iframes.length; i++) {
var iframe = iframes[i];
var frameDoc = iframe.contentDocument || iframe.contentWindow.document;

// 修改 iframe 中 body 的字体大小
frameDoc.body.style.fontSize = fontSize;
}
}

window.onload = function () {
// 重写 console.log,以便在 Xcode 中输出日志
Expand All @@ -442,35 +468,16 @@
};
}

if (isDarkMode()) {
// updateAllIframeColor(true);
// updateBackgroundColor(true);
}

updateAllIframeStyle();

var scrollHeight = getScrollHeight();

if (window.webkit && window.webkit.messageHandlers) {
// 调用 Objective-C 方法
window.webkit.messageHandlers.objcHandler.postMessage({
method: "getScrollHeight",
scrollHeight: scrollHeight,
});
}

var colorSchemeListener = window.matchMedia(
`(prefers-color-scheme: dark)`
);
colorSchemeListener.addEventListener(`change`, function (event) {
var isDarkMode = event.matches;
// updateAllIframeColor(isDarkMode);
// updateBackgroundColor(isDarkMode);
});
};
</script>
</head>

<body></body>

</html>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ NS_ASSUME_NONNULL_BEGIN

- (void)reset;

- (void)updateAllIframe;

@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "EZWebViewManager.h"
#import "EZConfiguration.h"

static NSString *kObjcHandler = @"objcHandler";
static NSString *kMethod = @"method";
Expand All @@ -27,30 +28,42 @@ - (WKWebView *)webView {
if (!_webView) {
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
[configuration.userContentController addScriptMessageHandler:self name:kObjcHandler];

_webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
}
return _webView;
}

#pragma mark - WKScriptMessageHandler

// 处理来自 JavaScript 的消息
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
id body = message.body;

if([message.name isEqualToString:kObjcHandler]) {
if([body[kMethod] isEqualToString:@"consoleLog"]) {
NSString *message = body[@"message"];
NSLog(@"<javascript log>: %@", message);
}

if([body[kMethod] isEqualToString:@"getScrollHeight"]) {
if([body[kMethod] isEqualToString:@"noteToUpdateScrollHeight"]) {
CGFloat scrollHeight = [body[@"scrollHeight"] floatValue];
if (self.didFinishUpdatingIframeHeightBlock) {
self.didFinishUpdatingIframeHeightBlock(scrollHeight);
}
}
}
}
}

#pragma mark - WebView evaluateJavaScript

- (void)updateAllIframe {
CGFloat fontSize = EZConfiguration.shared.fontSizeRatio * 100; // 140%
NSString *script = [NSString stringWithFormat:@"changeIframeBodyFontSize('%.1f%%'); updateAllIframeStyle();", fontSize];
[self.webView evaluateJavaScript:script completionHandler:^(id _Nullable result, NSError *_Nullable error) {
if (!error) {

}
}];
}

- (void)reset {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ - (void)refreshWithResult:(EZQueryResult *)result {
if (result.HTMLString.length) {
[self addSubview:self.webView];

if (result.webViewManager.isLoaded) {
[result.webViewManager updateAllIframe];
}

[result.webViewManager setDidFinishUpdatingIframeHeightBlock:^(CGFloat scrollHeight) {
mm_strongify(self);

Expand Down Expand Up @@ -1050,18 +1054,16 @@ - (BOOL)textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
NSLog(@"webView didFinishNavigation");

[self.result.webViewManager updateAllIframe];
}


- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error {
NSLog(@"didFailNavigation: %@", error);

}

/** 请求服务器发生错误 (如果是goBack时,当前页面也会回调这个方法,原因是NSURLErrorCancelled取消加载) */
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
NSLog(@"didFailProvisionalNavigation: %@", error);

}

// 监听 JavaScript 代码是否执行
Expand Down Expand Up @@ -1314,6 +1316,24 @@ - (void)getTextWithHref:(NSString *)href completionHandler:(void (^_Nullable)(NS
}];
}

- (void)updateWebViewAllIframeFontSize {
CGFloat fontSize = EZConfiguration.shared.fontSizeRatio * 100;

NSString *jsCode = [NSString stringWithFormat:
@"var iframes = document.querySelectorAll('iframe');"
@"for (var i = 0; i < iframes.length; i++) {"
@" var iframe = iframes[i];"
@" var frameDoc = iframe.contentDocument || iframe.contentWindow.document;"
@" frameDoc.body.style.fontSize = '%f%%';"
@"};", fontSize];

[self evaluateJavaScript:jsCode completionHandler:^(id _Nullable result, NSError *_Nullable error) {
if (!error) {

}
}];
}

#pragma mark -

// Convert text to multiple lines, such as "Hello world" to "Hello\nworld"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1257,19 +1257,19 @@ - (EZResultView *)resultCellAtRow:(NSInteger)row {
if ([service.serviceType isEqualToString:EZServiceTypeAppleDictionary]) {
EZAppleDictionary *appleDictService = (EZAppleDictionary *)service;

webView = result.webViewManager.webView;
EZWebViewManager *webViewManager = result.webViewManager;
webView = webViewManager.webView;
resultCell.wordResultView.webView = webView;

BOOL needLoadHTML = result.isShowing && result.HTMLString.length && !result.webViewManager.isLoaded;
BOOL needLoadHTML = result.isShowing && result.HTMLString.length && !webViewManager.isLoaded;
if (needLoadHTML) {
result.webViewManager.isLoaded = YES;
webViewManager.isLoaded = YES;

NSURL *htmlFileURL = [NSURL fileURLWithPath:appleDictService.htmlFilePath];
webView.navigationDelegate = resultCell.wordResultView;
[webView loadFileURL:htmlFileURL allowingReadAccessToURL:TTTDictionary.userDictionaryDirectoryURL];
} else if (result.webViewManager.needUpdateIframeHeight && result.webViewManager.isLoaded) {
NSString *script = @"updateAllIframeStyle();";
[webView evaluateJavaScript:script completionHandler:nil];
} else if (webViewManager.needUpdateIframeHeight && webViewManager.isLoaded) {
[webViewManager updateAllIframe];
}
}

Expand Down

0 comments on commit 5b4466a

Please sign in to comment.