From 95cec2c6481772f8d448b4cf98555ea91690b448 Mon Sep 17 00:00:00 2001 From: Simone Manganelli Date: Wed, 22 Apr 2009 11:34:21 +0800 Subject: [PATCH] Add ability to download H.264 file from YouTube Signed-off-by: Jonathan 'Wolf' Rentzsch --- Plugin/Plugin.h | 2 ++ Plugin/Plugin.m | 55 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/Plugin/Plugin.h b/Plugin/Plugin.h index 4da83ae6..dd856f52 100644 --- a/Plugin/Plugin.h +++ b/Plugin/Plugin.h @@ -61,6 +61,8 @@ THE SOFTWARE. - (IBAction)loadH264:(id)sender; - (IBAction)loadAllOnPage:(id)sender; +- (IBAction)downloadH264:(id)sender; + - (BOOL) isConsideredInvisible; - (void) _convertTypesForContainer; diff --git a/Plugin/Plugin.m b/Plugin/Plugin.m index c01db829..903c0702 100644 --- a/Plugin/Plugin.m +++ b/Plugin/Plugin.m @@ -405,8 +405,15 @@ - (NSMenu*) menuForEvent: (NSEvent*) event if ([self _hasH264Version]) { [[self menu] insertItemWithTitle: NSLocalizedString( @"Load H.264", "Load H.264 context menu item" ) action: @selector( loadH264: ) keyEquivalent: @"" atIndex: 1]; + [[self menu] insertItemWithTitle: NSLocalizedString( @"Download H.264", "Download H.264 menu item" ) + action: @selector( downloadH264: ) keyEquivalent: @"" atIndex: 2]; [[[self menu] itemAtIndex: 1] setTarget: self]; - } + } else if (_fromYouTube) { + // has no H.264 version but is from YouTube; it's an embedded view! + + [[self menu] insertItemWithTitle: NSLocalizedString ( @"Load YouTube.com page for this video", "Load YouTube page menu item" ) + action: @selector (loadYouTubePage: ) keyEquivalent: @"" atIndex: 1]; + } } } @@ -750,6 +757,52 @@ - (void) _convertToMP4ContainerAfterDelay self.container = nil; } +- (IBAction)downloadH264:(id)sender +{ + NSString* video_id = [ self _videoId ]; + NSString* video_hash = [ self _videoHash ]; + + NSString* src = [ NSString stringWithFormat: @"http://www.youtube.com/get_video?fmt=18&video_id=%@&t=%@", + video_id, video_hash ]; + + NSString *appBundleIdentifier = [[NSBundle mainBundle] bundleIdentifier]; + + if ([appBundleIdentifier isEqualToString:@"com.apple.Safari"]) { + // additional tests need to be performed, because this can indicate + // either WebKit *or* Safari; according to @bdash on Twitter, we need + // to check whether the framework bundle that we're using is + // contained within WebKit.app or not + + // however, the user may have renamed the bundle, so we have to get + // its path, then get its bundle identifier + + NSString *privateFrameworksPath = [[NSBundle bundleForClass:[WebView class]] privateFrameworksPath]; + + NSScanner *pathScanner = [[NSScanner alloc] initWithString:privateFrameworksPath]; + NSString *pathString = nil; + [pathScanner scanUpToString:@".app" intoString:&pathString]; + NSBundle *testBundle = [[NSBundle alloc] initWithPath:[pathString stringByAppendingPathExtension:@"app"]]; + NSString *testBundleIdentifier = [testBundle bundleIdentifier]; + [testBundle release]; + [pathScanner release]; + + + // Safari uses the framework inside /System/Library/Frameworks/ , and + // since there's no ".app" extension in that path, the resulting + // bundle identifier will be nil; however, if it's WebKit, there *will* + // be a ".app" in the frameworks path, and we'll get a valid bundle + // identifier to launch with + + if (testBundleIdentifier != nil) appBundleIdentifier = testBundleIdentifier; + } + + [[NSWorkspace sharedWorkspace] openURLs:[NSArray arrayWithObject:[NSURL URLWithString:src]] + withAppBundleIdentifier:appBundleIdentifier + options:NSWorkspaceLaunchDefault + additionalEventParamDescriptor:[NSAppleEventDescriptor nullDescriptor] + launchIdentifiers:nil]; +} + #pragma mark - #pragma mark DOM Conversion