Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
WKWebView とめんどくさいお友達
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Megabits_mzq
February 22, 2022
Programming
780
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
WKWebView とめんどくさいお友達
Swift 愛好会 2022/2/22 登壇資料
Megabits_mzq
February 22, 2022
More Decks by Megabits_mzq
See All by Megabits_mzq
OTP を自動で入力する裏技
megabitsenmzq
0
170
SwiftUI と Shader を活用した楽しいオンボーディング起動画面の作成
megabitsenmzq
0
140
Liquid Glass, どこが変わったのか
megabitsenmzq
0
180
iPhone 16 Camera Control
megabitsenmzq
0
160
240fps で画像処理したい
megabitsenmzq
0
240
Swift 開発が楽になる道具たち
megabitsenmzq
1
790
Animoji を作ってみた
megabitsenmzq
0
210
MainMenu.xib を翻訳してみた
megabitsenmzq
0
310
先週解決した SwiftUI 問題
megabitsenmzq
0
160
Other Decks in Programming
See All in Programming
アルゴリズムは何を圧縮しているのか ─ Haskell から育った「圧縮代数」というメンタルモデル
naoya
16
3.6k
自作OSでスライド発表する
uyuki234
1
3.9k
Haskell/Servantを通してWebミドルウェアを捉え直す
pizzacat83
1
610
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
140
yield再入門 #phpcon
o0h
PRO
0
700
Hatena Engineer Seminar #37「言語モデルの活用に関する研究」
slashnephy
0
540
才能?センス?知らん、 続けたもん勝ちだ。-- 結婚・出産・癌を越えてなお、私がプロダクトを創り続ける理由
16bitidol
2
900
フィードバックで育てるAI開発
kotaminato
1
120
Prismを使った型安全な暗号化_関数型まつり2026
_fhhmm
0
150
GitHubCopilotCLIのスラッシュコマンドを自作してみる
htkym
0
100
トークンをケチるな、設計しろ:GitHub Copilotを賢く使うコンテキスト戦略
ochtum
0
330
Lean は証明の正しさを確認するためだけのツールって思ってませんか?
inoueasei
1
100
Featured
See All Featured
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.4k
Test your architecture with Archunit
thirion
1
2.3k
Believing is Seeing
oripsolob
1
170
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
370
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.3k
The Curious Case for Waylosing
cassininazir
1
440
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
490
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
190
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.1k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
201
75k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
380
Transcript
Jinyu Meng (Megabits Studio) WKWebView とめんどくさいお友達
個⼈紹介 独 ⽴ 開 発 者 で す ! Megabits
⾦⿂
個⼈紹介 Twitter @Megabits_mzq @Megabits_Studio
None
None
“WebKit を触ると、不幸になる” ──マハトマ・ガンディー が⾔ったことがない
None
self.view.window?.level = .floating
None
None
let width = view.frame.width let height = view.frame.height -
toolbarView.frame.maxY let script1 = """ Object.defineProperty(window, "screenTop", { get: function(){ return 0; }}); Object.defineProperty(window, "outerWidth", { get: function(){ return \(width); }}); Object.defineProperty(window, "outerHeight", { get: function(){ return \(height); }}); """ let userScript1 = WKUserScript(source: script1, injectionTime: .atDocumentStart, forMainFrameOnly: true) webView.configuration.userContentController.addUserScript(userScript1)
#import <AppKit/AppKit.h> #import <objc/runtime.h> static void HookMessage(Class cls, SEL selName,
IMP replaced, IMP *orig) { Method origMethod = class_getInstanceMethod(cls, selName); if (!origMethod) {return;} *orig = method_setImplementation(origMethod, replaced); } static NSRect (*original_NSScreen_frame)(NSScreen *self, SEL _cmd); static NSRect replaced_NSScreen_frame(NSScreen *self, SEL _cmd) { //https://stackoverflow.com/questions/1451342/objective-c-find-caller-of-method NSString *sourceString = [[NSThread callStackSymbols] objectAtIndex:1]; NSCharacterSet *separatorSet = [NSCharacterSet characterSetWithCharactersInString:@" -[]+?.,"]; NSMutableArray *array = [NSMutableArray arrayWithArray:[sourceString componentsSeparatedByCharactersInSet:separatorSet]]; [array removeObject:@""]; NSString *framework = [array objectAtIndex:1]; NSRect origFrame = original_NSScreen_frame(self, _cmd); if (![framework isEqualTo:@"WebCore"] { return origFrame; } return CGRectMake(0, 0, 0, 0); } #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wundeclared-selector" __attribute__((constructor)) static void makeMyMagicWork() { HookMessage( objc_getClass("NSScreen"), NSSelectorFromString(@"frame"), (IMP)&replaced_NSScreen_frame, (IMP *)&original_NSScreen_frame ); } #pragma clang diagnostic pop
None
None
None
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy)
-> Void) { if navigationResponse.canShowMIMEType, let mimeType = navigationResponse.response.mimeType { let mainType = mimeType.components(separatedBy: "/")[0] // print(mainType, mimeType, navigationResponse.isForMainFrame) if !["image", "audio", "video"].contains(mainType) { if !(["text/plain", "text/xml"].contains(mimeType) && navigationResponse.isForMainFrame){ decisionHandler(.allow) return } } else if !navigationResponse.isForMainFrame { decisionHandler(.allow) return } } if let fileName = navigationResponse.response.suggestedFilename, let url = navigationResponse.response.url { webView.configuration.websiteDataStore.httpCookieStore.getAllCookies() { cookies in print(fileName) self.createNewDownload(url: url, fileName: fileName, cookies: cookies) } } decisionHandler(.cancel) }
MIME Type • text/plain • text/xml • Image/png • Audio/mp3
• Video/mp4 • … navigationResponse.canShowMIMEType, navigationResponse.response.mimeType
navigationResponse.isForMainFrame
Blob 👉 WKDownloadDelegate
None
override func willOpenMenu(_ menu: NSMenu, with event: NSEvent) { super.willOpenMenu(menu,
with: event) for (index, menuItem) in menu.items.enumerated() { switch menuItem.identifier?.rawValue { case "WKMenuItemIdentifierCopyImage": menu.removeItem(menuItem) default: break // print(menuItem.identifier?.rawValue ?? "") } } }
window.oncontextmenu = (event) => { var target = event.target var
href = target.href var parentElement = target while (href == null && parentElement.parentElement != null) { parentElement = parentElement.parentElement href = parentElement.href } if (href == null) { parentElement = null; } window.webkit.messageHandlers.oncontextmenu.postMessage({ nodeName: target.nodeName, id: target.id, src: target.src, hrefNodeName: parentElement?.nodeName, hrefId: parentElement?.id, href }); } """ https://stackover fl ow.com/a/66836354/1711103
None
@Megabits_mzq @Megabits_Studio https://menubarx.app