Skip to content

Instantly share code, notes, and snippets.

@niw
Last active August 29, 2015 14:09
Show Gist options
  • Save niw/586a94af026efd93825d to your computer and use it in GitHub Desktop.
Save niw/586a94af026efd93825d to your computer and use it in GitHub Desktop.
Show favicon on Safari tabs.
/*
SafariHack
==========
Use with SIMBL or compatible extension like EasySIMBL. Only tested on OS X 10.10.
LICENSE
-------
Copyright (c) 2014 Yoshimasa Niwa
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#import <Cocoa/Cocoa.h>
#import <objc/message.h>
void objc_exchangeInstanceMethodImplementations(const char* className, SEL originlSelector, SEL replacementSelector)
{
Class class = objc_getClass(className);
Method originalMethod = class_getInstanceMethod(class, originlSelector);
Method replacementMethod = class_getInstanceMethod(class, replacementSelector);
method_exchangeImplementations(originalMethod, replacementMethod);
}
#pragma mark - WebKit
typedef const void *WKTypeRef;
void WKRelease(WKTypeRef type);
typedef const struct OpaqueWKFrame *WKFrameRef;
typedef const struct OpaqueWKPage *WKPageRef;
typedef const struct OpaqueWKURL *WKURLRef;
WKFrameRef WKPageGetMainFrame(WKPageRef page);
WKURLRef WKFrameCopyURL(WKFrameRef frame);
CFURLRef WKURLCopyCFURL(CFAllocatorRef alloc, WKURLRef URL) CF_RETURNS_RETAINED;
typedef struct WKSize {
double width;
double height;
} WKSize;
typedef const struct OpaqueWKContext* WKContextRef;
typedef const struct OpaqueWKIconDatabase* WKIconDatabaseRef;
WKURLRef WKURLCreateWithUTF8CString(const char* string);
WKContextRef WKPageGetContext(WKPageRef page);
WKIconDatabaseRef WKContextGetIconDatabase(WKContextRef context);
CGImageRef WKIconDatabaseTryGetCGImageForURL(WKIconDatabaseRef iconDatabase, WKURLRef url, WKSize size);
@interface WKView : NSView <NSTextInputClient>
@end
@interface WKView (Private)
@property (nonatomic, readonly) WKPageRef pageRef;
@end
#pragma mark - Safari
@interface ScrollableTabBarViewButton : NSButton
@end
@interface ScrollableTabButton : ScrollableTabBarViewButton
@end
@interface BrowserTabViewItem : NSTabViewItem
- (WKView *)wkView;
- (ScrollableTabButton *)scrollableTabButton;
- (void)updateLabelNow;
@end
#pragma mark - SafariHack
@interface NSView (SafariHack)
- (NSURL *)sfh_mainFrameURL;
- (NSImage *)sfh_iconImageForURL:(NSURL *)url;
@end
@interface NSTabViewItem (SafariHack)
- (ScrollableTabButton *)sfh_scrollableTabButton;
- (void)sfh_updateLabelNow;
@end
@interface SFHTabViewItemAdapter : NSObject
- (instancetype)initWithBrowserTabViewItem:(BrowserTabViewItem *)tabViewItem;
- (void)updateScrollableTabButtonIfNeeded:(ScrollableTabButton *)tabButton;
- (void)updateIconView;
@end
@interface SafariHack : NSObject
+ (void)load;
@end
#pragma mark -
@implementation NSView (SafariHack)
- (NSURL *)sfh_mainFrameURL
{
if ([self isKindOfClass:[WKView class]]) {
WKView *wkView = (WKView *)self;
WKPageRef wkPage = wkView.pageRef;
if (wkPage) {
WKFrameRef wkFrame = WKPageGetMainFrame(wkPage);
if (wkFrame) {
WKURLRef wkURL = WKFrameCopyURL(wkFrame);
if (wkURL) {
NSURL *url = (__bridge NSURL *)WKURLCopyCFURL(CFAllocatorGetDefault(), wkURL);
WKRelease(wkURL);
return url;
}
}
}
}
return nil;
}
- (NSImage *)sfh_iconImageForURL:(NSURL *)url;
{
if ([self isKindOfClass:[WKView class]]) {
WKView *wkView = (WKView *)self;
WKPageRef wkPage = wkView.pageRef;
if (wkPage) {
WKURLRef wkURL = WKURLCreateWithUTF8CString([[url absoluteString] UTF8String]);
WKContextRef wkContext = WKPageGetContext(wkPage);
WKIconDatabaseRef wkIconDatabase = WKContextGetIconDatabase(wkContext);
WKSize size = (WKSize){.width = 32.0, .height = 32.0};
CGImageRef icon = WKIconDatabaseTryGetCGImageForURL(wkIconDatabase, wkURL, size);
WKRelease(wkURL);
if (icon) {
CGFloat width = CGImageGetWidth(icon);
CGFloat height = CGImageGetHeight(icon);
NSImage *image = [[NSImage alloc] initWithCGImage:icon size:NSSizeFromCGSize(CGSizeMake(width, height))];
return image;
}
}
}
return nil;
}
@end
#pragma mark -
@implementation NSTabViewItem (SafariHack)
static const void * const kSFHBrowserTabViewItemAdapterKey = &kSFHBrowserTabViewItemAdapterKey;
- (ScrollableTabButton *)sfh_scrollableTabButton
{
ScrollableTabButton *tabButton = [self sfh_scrollableTabButton];
if (tabButton && !objc_getAssociatedObject(self, kSFHBrowserTabViewItemAdapterKey)) {
SFHTabViewItemAdapter *adapter = [[SFHTabViewItemAdapter alloc] initWithBrowserTabViewItem:(BrowserTabViewItem *)self];
[adapter updateScrollableTabButtonIfNeeded:tabButton];
objc_setAssociatedObject(self, kSFHBrowserTabViewItemAdapterKey, adapter, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return tabButton;
}
- (void)sfh_updateLabelNow
{
[self sfh_updateLabelNow];
SFHTabViewItemAdapter *adapter = objc_getAssociatedObject(self, kSFHBrowserTabViewItemAdapterKey);
if (adapter) {
[adapter updateIconView];
}
}
@end
#pragma mark -
@implementation SFHTabViewItemAdapter
{
BrowserTabViewItem *_tabViewItem;
NSImageView *_iconView;
}
- (instancetype)initWithBrowserTabViewItem:(BrowserTabViewItem *)tabViewItem
{
if (self = [super init]) {
_tabViewItem = tabViewItem;
}
return self;
}
- (void)updateScrollableTabButtonIfNeeded:(ScrollableTabButton *)tabButton
{
if (tabButton && !_iconView) {
_iconView = [[NSImageView alloc] initWithFrame:CGRectMake(4.0, 4.0, 16.0, 16.0)];
[tabButton addSubview:_iconView];
}
}
- (void)updateIconView
{
NSURL *url = [_tabViewItem.wkView sfh_mainFrameURL];
if (url && _iconView) {
NSImage *iconImage = [_tabViewItem.wkView sfh_iconImageForURL:url];
if (iconImage) {
_iconView.image = iconImage;
}
}
}
@end
@implementation SafariHack
#define EXCHANGE_METHOD(class, method) objc_exchangeInstanceMethodImplementations(class, @selector(method), @selector(sfh_##method))
+ (void)load
{
EXCHANGE_METHOD("BrowserTabViewItem", scrollableTabButton);
EXCHANGE_METHOD("BrowserTabViewItem", updateLabelNow);
NSLog(@"Safari Hack is loaded.");
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment