Skip to content

Commit 95cec2c

Browse files
Simone Manganellirentzsch
Simone Manganelli
authored andcommitted
Add ability to download H.264 file from YouTube
Signed-off-by: Jonathan 'Wolf' Rentzsch <[email protected]>
1 parent dde839f commit 95cec2c

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

Plugin/Plugin.h

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ THE SOFTWARE.
6161
- (IBAction)loadH264:(id)sender;
6262
- (IBAction)loadAllOnPage:(id)sender;
6363

64+
- (IBAction)downloadH264:(id)sender;
65+
6466
- (BOOL) isConsideredInvisible;
6567

6668
- (void) _convertTypesForContainer;

Plugin/Plugin.m

+54-1
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,15 @@ - (NSMenu*) menuForEvent: (NSEvent*) event
405405
if ([self _hasH264Version]) {
406406
[[self menu] insertItemWithTitle: NSLocalizedString( @"Load H.264", "Load H.264 context menu item" )
407407
action: @selector( loadH264: ) keyEquivalent: @"" atIndex: 1];
408+
[[self menu] insertItemWithTitle: NSLocalizedString( @"Download H.264", "Download H.264 menu item" )
409+
action: @selector( downloadH264: ) keyEquivalent: @"" atIndex: 2];
408410
[[[self menu] itemAtIndex: 1] setTarget: self];
409-
}
411+
} else if (_fromYouTube) {
412+
// has no H.264 version but is from YouTube; it's an embedded view!
413+
414+
[[self menu] insertItemWithTitle: NSLocalizedString ( @"Load YouTube.com page for this video", "Load YouTube page menu item" )
415+
action: @selector (loadYouTubePage: ) keyEquivalent: @"" atIndex: 1];
416+
}
410417
}
411418
}
412419

@@ -750,6 +757,52 @@ - (void) _convertToMP4ContainerAfterDelay
750757
self.container = nil;
751758
}
752759

760+
- (IBAction)downloadH264:(id)sender
761+
{
762+
NSString* video_id = [ self _videoId ];
763+
NSString* video_hash = [ self _videoHash ];
764+
765+
NSString* src = [ NSString stringWithFormat: @"http://www.youtube.com/get_video?fmt=18&video_id=%@&t=%@",
766+
video_id, video_hash ];
767+
768+
NSString *appBundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
769+
770+
if ([appBundleIdentifier isEqualToString:@"com.apple.Safari"]) {
771+
// additional tests need to be performed, because this can indicate
772+
// either WebKit *or* Safari; according to @bdash on Twitter, we need
773+
// to check whether the framework bundle that we're using is
774+
// contained within WebKit.app or not
775+
776+
// however, the user may have renamed the bundle, so we have to get
777+
// its path, then get its bundle identifier
778+
779+
NSString *privateFrameworksPath = [[NSBundle bundleForClass:[WebView class]] privateFrameworksPath];
780+
781+
NSScanner *pathScanner = [[NSScanner alloc] initWithString:privateFrameworksPath];
782+
NSString *pathString = nil;
783+
[pathScanner scanUpToString:@".app" intoString:&pathString];
784+
NSBundle *testBundle = [[NSBundle alloc] initWithPath:[pathString stringByAppendingPathExtension:@"app"]];
785+
NSString *testBundleIdentifier = [testBundle bundleIdentifier];
786+
[testBundle release];
787+
[pathScanner release];
788+
789+
790+
// Safari uses the framework inside /System/Library/Frameworks/ , and
791+
// since there's no ".app" extension in that path, the resulting
792+
// bundle identifier will be nil; however, if it's WebKit, there *will*
793+
// be a ".app" in the frameworks path, and we'll get a valid bundle
794+
// identifier to launch with
795+
796+
if (testBundleIdentifier != nil) appBundleIdentifier = testBundleIdentifier;
797+
}
798+
799+
[[NSWorkspace sharedWorkspace] openURLs:[NSArray arrayWithObject:[NSURL URLWithString:src]]
800+
withAppBundleIdentifier:appBundleIdentifier
801+
options:NSWorkspaceLaunchDefault
802+
additionalEventParamDescriptor:[NSAppleEventDescriptor nullDescriptor]
803+
launchIdentifiers:nil];
804+
}
805+
753806

754807
#pragma mark -
755808
#pragma mark DOM Conversion

0 commit comments

Comments
 (0)