Last active
October 12, 2018 07:30
-
-
Save ChiChou/884c9d337d9554a3a619cc8e0ebd355f to your computer and use it in GitHub Desktop.
If you don't want invert color web for Dark mode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// clang -shared -undefined dynamic_lookup -o /Applications/Dash.app/Contents/MacOS/libDash.dylib Dash.m | |
// optool install -c load -p @executable_path/libDash.dylib -t /Applications/Dash.app/Contents/MacOS/Dash | |
#import <Foundation/Foundation.h> | |
#import <objc/runtime.h> | |
static void pleasedontinvertwebview(/* we don't care about the args */) { | |
NSLog(@"oops"); | |
} | |
typedef void (*OriginalSetUserCSSImp)(id self, SEL sel, NSURL *url); | |
static OriginalSetUserCSSImp originalImp; | |
static void pleasedontinvertimages(id self, SEL sel, NSURL *url) { | |
NSLog(@"oops"); | |
NSError *err = nil; | |
NSString *content = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&err]; | |
if (err) return; | |
NSArray *lines = [content componentsSeparatedByString:@"\n"]; | |
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (SELF contains[c] 'invert()')"]; | |
NSArray *filtered = [lines filteredArrayUsingPredicate:predicate]; | |
NSString *style = [filtered componentsJoinedByString:@"\n"]; | |
[style writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:nil]; | |
originalImp(self, sel, url); | |
} | |
__attribute__((constructor)) static void webview() { | |
Method m = class_getInstanceMethod(NSClassFromString(@"DHWebView"), | |
NSSelectorFromString(@"setContentFilters:")); | |
if (m) method_setImplementation(m, (IMP)pleasedontinvertwebview); | |
m = class_getInstanceMethod(NSClassFromString(@"WebPreferences"), | |
NSSelectorFromString(@"setUserStyleSheetLocation:")); | |
if (m) { | |
originalImp = (OriginalSetUserCSSImp)method_getImplementation(m); | |
method_setImplementation(m, (IMP)pleasedontinvertimages); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment