Skip to content

Instantly share code, notes, and snippets.

@SKaplanOfficial
Created June 30, 2023 18:47
Show Gist options
  • Save SKaplanOfficial/b82f959c18ccaf5b34c20c6763ab14ce to your computer and use it in GitHub Desktop.
Save SKaplanOfficial/b82f959c18ccaf5b34c20c6763ab14ce to your computer and use it in GitHub Desktop.
(() => {
ObjC.import("AppKit");
ObjC.import("WebKit");
ObjC.import("objc");
app = Application("iCab");
const baseURL = app.windows[0].currentTab.url();
// Size of WebView
const width = 1080;
const height = 720;
const WKNavigationDelegate = $.objc_getProtocol("WKNavigationDelegate");
const _NSApp = $.NSApplication.sharedApplication;
if (!$["WebViewDelegate"]) {
ObjC.registerSubclass({
name: "WebViewDelegate",
superclass: "NSObject",
protocols: ["WKNavigationDelegate"],
properties: {
result: "id",
},
methods: {
"webView:didFinishNavigation:": {
types: ["void", ["id", "id"]],
implementation: function (webview, navigation) {
// Run JS to get the HTML of the document
let jsString = "document.documentElement.outerHTML;";
webview.evaluateJavaScriptCompletionHandler(
jsString,
(result, error) => {
if (error.localizedDescription) {
$.NSLog(error.localizedDescription);
this.result = $.NSString.stringWithString("Error");
return;
}
this.result = result;
}
);
},
},
},
});
}
const frame = $.NSMakeRect(0, 0, width, height / 2);
const config = $.WKWebViewConfiguration.alloc.init;
const view = $.WKWebView.alloc.initWithFrameConfiguration(frame, config);
const delegate = $.WebViewDelegate.alloc.init;
view.navigationDelegate = delegate;
// Create request and set HTTP header(s)
const request = $.NSMutableURLRequest.requestWithURL(
$.NSURL.URLWithString(baseURL)
);
request.setValueForHTTPHeaderField(
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.3 Safari/605.1.15",
"User-Agent"
);
const nav = view.loadRequest(request);
// Wait for HTML source
while (delegate.result.js == undefined) {
runLoop = $.NSRunLoop.currentRunLoop;
today = $.NSDate.dateWithTimeIntervalSinceNow(0.1);
runLoop.runUntilDate(today);
}
return delegate.result.js;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment