Skip to content

Commit a055047

Browse files
Simone Manganellirentzsch
Simone Manganelli
authored andcommitted
new contextual menu item that opens the YouTube.com page for embedded YouTube players
Signed-off-by: Jonathan 'Wolf' Rentzsch <[email protected]>
1 parent 95cec2c commit a055047

File tree

2 files changed

+97
-29
lines changed

2 files changed

+97
-29
lines changed

Plugin/Plugin.h

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ THE SOFTWARE.
4444
NSString *_baseURL;
4545
NSDictionary *_attributes;
4646
NSDictionary *_originalOpacityAttributes;
47+
NSString *_src;
48+
NSString *_videoId;
49+
NSString *_launchedAppBundleIdentifier;
4750
}
4851

4952
+ (NSView *)plugInViewWithArguments:(NSDictionary *)arguments;
@@ -56,6 +59,9 @@ THE SOFTWARE.
5659
@property (retain) NSString *baseURL;
5760
@property (nonatomic, retain) NSDictionary *attributes;
5861
@property (retain) NSDictionary *originalOpacityAttributes;
62+
@property (retain) NSString *src;
63+
@property (retain) NSString *videoId;
64+
@property (retain) NSString *launchedAppBundleIdentifier;
5965

6066
- (IBAction)loadFlash:(id)sender;
6167
- (IBAction)loadH264:(id)sender;

Plugin/Plugin.m

+91-29
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,10 @@ - (void) _loadContent: (NSNotification*) notification;
5959
- (void) _loadContentForWindow: (NSNotification*) notification;
6060

6161
- (NSDictionary*) _flashVarDictionary: (NSString*) flashvarString;
62+
- (NSString*) flashvarWithName: (NSString*) argName;
6263
- (BOOL) _hasH264Version;
6364
- (BOOL) _useH264Version;
65+
- (NSString *)launchedAppBundleIdentifier;
6466
@end
6567

6668

@@ -97,15 +99,18 @@ - (id) initWithArguments:(NSDictionary *)arguments
9799
if (![[NSUserDefaults standardUserDefaults] objectForKey:sPluginEnabled]) {
98100
// Default to enable the plugin
99101
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:sPluginEnabled];
100-
}
102+
}
103+
104+
self.launchedAppBundleIdentifier = [self launchedAppBundleIdentifier];
101105

102106
self.webView = [[[arguments objectForKey:WebPlugInContainerKey] webFrame] webView];
103107

104108
self.container = [arguments objectForKey:WebPlugInContainingElementKey];
105109

106110
[self _migrateWhitelist];
107111

108-
// Get URL and test against the whitelist
112+
113+
// Get URL
109114

110115
NSURL *base = [arguments objectForKey:WebPlugInBaseURLKey];
111116
self.baseURL = [base absoluteString];
@@ -114,29 +119,72 @@ - (id) initWithArguments:(NSDictionary *)arguments
114119
self.attributes = [arguments objectForKey:WebPlugInAttributesKey];
115120
NSString *srcAttribute = [self.attributes objectForKey:@"src"];
116121

122+
if (srcAttribute) {
123+
self.src = srcAttribute;
124+
} else {
125+
NSString *dataAttribute = [self.attributes objectForKey:@"data"];
126+
if (dataAttribute) self.src = dataAttribute;
127+
}
128+
129+
NSLog(@"self.src: %@",self.src);
130+
131+
132+
// set tooltip
133+
134+
if (self.src) [self setToolTip:self.src];
135+
136+
117137
// Read in flashvars (needed to determine YouTube videos)
118138

119139
NSString* flashvars = [ self.attributes objectForKey: @"flashvars" ];
120140
if( flashvars != nil )
121141
_flashVars = [ [ self _flashVarDictionary: flashvars ] retain ];
122142

123-
#if LOGGING_ENABLED
143+
#if 1
124144
NSLog( @"arguments = %@", arguments );
125145
NSLog( @"flashvars = %@", _flashVars );
126146
#endif
147+
148+
// check whether it's from YouTube and get the video_id
149+
150+
_fromYouTube = [self.host isEqualToString:@"www.youtube.com"]
151+
|| ( flashvars != nil && [flashvars rangeOfString: @"www.youtube.com"].location != NSNotFound )
152+
|| (self.src != nil && [self.src rangeOfString: @"youtube.com"].location != NSNotFound );
153+
154+
if (_fromYouTube) {
155+
NSString *videoId = [ self flashvarWithName: @"video_id" ];
156+
if (videoId != nil) {
157+
self.videoId = videoId;
158+
} else {
159+
// scrub the URL to determine the video_id
160+
161+
NSString *videoIdFromURL = nil;
162+
NSScanner *URLScanner = [[NSScanner alloc] initWithString:self.src];
163+
[URLScanner scanUpToString:@"youtube.com/v/" intoString:nil];
164+
if ([URLScanner scanString:@"youtube.com/v/" intoString:nil]) {
165+
// URL is in required format, next characters are the id
166+
167+
[URLScanner scanUpToString:@"&" intoString:&videoIdFromURL];
168+
if (videoIdFromURL) self.videoId = videoIdFromURL;
169+
}
170+
[URLScanner release];
171+
}
172+
}
173+
174+
175+
// check whether plugin is disabled, load all content as normal if so
176+
127177
if ( ![ [ NSUserDefaults standardUserDefaults ] boolForKey: sPluginEnabled ] ) {
128-
// plugin is disabled, load all content as normal
129178
_isLoadingFromWhitelist = YES;
130179
[self _convertTypesForContainer];
131180
return self;
132181
}
133-
134-
_fromYouTube = [self.host isEqualToString:@"www.youtube.com"]
135-
|| ( flashvars != nil && [flashvars rangeOfString: @"www.youtube.com"].location != NSNotFound );
136-
182+
183+
137184
// Set up main menus
138185

139186
[ CTFMenubarMenuController sharedController ]; // trigger the menu items to be added
187+
140188

141189
// Check for sIFR
142190

@@ -165,6 +213,7 @@ - (id) initWithArguments:(NSDictionary *)arguments
165213

166214
BOOL loadFromWhiteList = [self _isHostWhitelisted];
167215

216+
168217
// Check the SWF src URL itself against the whitelist (allows embbeded videos from whitelisted sites to play, e.g. YouTube)
169218

170219
if( !loadFromWhiteList )
@@ -178,6 +227,7 @@ - (id) initWithArguments:(NSDictionary *)arguments
178227
}
179228
}
180229
}
230+
181231

182232
// Handle if this is loading from whitelist
183233

@@ -187,15 +237,6 @@ - (id) initWithArguments:(NSDictionary *)arguments
187237
return self;
188238
}
189239

190-
// Set tooltip
191-
192-
if (srcAttribute)
193-
[self setToolTip:srcAttribute];
194-
else {
195-
NSString *src = [self.attributes objectForKey:@"data"];
196-
if (src)
197-
[self setToolTip:src];
198-
}
199240

200241
// send a notification so that all flash objects can be tracked
201242
// we only want to track it if we don't auto-load it
@@ -408,11 +449,13 @@ - (NSMenu*) menuForEvent: (NSEvent*) event
408449
[[self menu] insertItemWithTitle: NSLocalizedString( @"Download H.264", "Download H.264 menu item" )
409450
action: @selector( downloadH264: ) keyEquivalent: @"" atIndex: 2];
410451
[[[self menu] itemAtIndex: 1] setTarget: self];
452+
[[[self menu] itemAtIndex: 2] setTarget: self];
411453
} else if (_fromYouTube) {
412454
// has no H.264 version but is from YouTube; it's an embedded view!
413455

414456
[[self menu] insertItemWithTitle: NSLocalizedString ( @"Load YouTube.com page for this video", "Load YouTube page menu item" )
415457
action: @selector (loadYouTubePage: ) keyEquivalent: @"" atIndex: 1];
458+
[[[self menu] itemAtIndex: 1] setTarget: self];
416459
}
417460
}
418461
}
@@ -686,10 +729,10 @@ - (NSString*) flashvarWithName: (NSString*) argName
686729
return [ _flashVars objectForKey: argName ];
687730
}
688731

689-
- (NSString*) _videoId
732+
/*- (NSString*) _videoId
690733
{
691734
return [ self flashvarWithName: @"video_id" ];
692-
}
735+
}*/
693736

694737
- (NSString*) _videoHash
695738
{
@@ -699,7 +742,7 @@ - (NSString*) _videoHash
699742
- (BOOL) _hasH264Version
700743
{
701744
if( _fromYouTube )
702-
return [ self _videoId ] != nil && [ self _videoHash ] != nil;
745+
return self.videoId != nil && [ self _videoHash ] != nil;
703746
else
704747
return NO;
705748
}
@@ -713,7 +756,7 @@ - (BOOL) _useH264Version
713756

714757
- (void) _convertElementForMP4: (DOMElement*) element
715758
{
716-
NSString* video_id = [ self _videoId ];
759+
NSString* video_id = self.videoId;
717760
NSString* video_hash = [ self _videoHash ];
718761

719762
NSString* src = [ NSString stringWithFormat: @"http://www.youtube.com/get_video?fmt=18&video_id=%@&t=%@",
@@ -757,14 +800,8 @@ - (void) _convertToMP4ContainerAfterDelay
757800
self.container = nil;
758801
}
759802

760-
- (IBAction)downloadH264:(id)sender
803+
- (NSString *)launchedAppBundleIdentifier
761804
{
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-
768805
NSString *appBundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
769806

770807
if ([appBundleIdentifier isEqualToString:@"com.apple.Safari"]) {
@@ -796,8 +833,30 @@ - (IBAction)downloadH264:(id)sender
796833
if (testBundleIdentifier != nil) appBundleIdentifier = testBundleIdentifier;
797834
}
798835

836+
return appBundleIdentifier;
837+
}
838+
839+
- (IBAction)downloadH264:(id)sender
840+
{
841+
NSString* video_id = self.videoId;
842+
NSString* video_hash = [ self _videoHash ];
843+
844+
NSString* src = [ NSString stringWithFormat: @"http://www.youtube.com/get_video?fmt=18&video_id=%@&t=%@",
845+
video_id, video_hash ];
846+
799847
[[NSWorkspace sharedWorkspace] openURLs:[NSArray arrayWithObject:[NSURL URLWithString:src]]
800-
withAppBundleIdentifier:appBundleIdentifier
848+
withAppBundleIdentifier:self.launchedAppBundleIdentifier
849+
options:NSWorkspaceLaunchDefault
850+
additionalEventParamDescriptor:[NSAppleEventDescriptor nullDescriptor]
851+
launchIdentifiers:nil];
852+
}
853+
854+
- (IBAction)loadYouTubePage:(id)sender
855+
{
856+
NSString* YouTubePageURL = [ NSString stringWithFormat: @"http://www.youtube.com/watch?v=%@",self.videoId ];
857+
858+
[[NSWorkspace sharedWorkspace] openURLs:[NSArray arrayWithObject:[NSURL URLWithString:YouTubePageURL]]
859+
withAppBundleIdentifier:self.launchedAppBundleIdentifier
801860
options:NSWorkspaceLaunchDefault
802861
additionalEventParamDescriptor:[NSAppleEventDescriptor nullDescriptor]
803862
launchIdentifiers:nil];
@@ -882,5 +941,8 @@ - (void) _revertToOriginalOpacityAttributes
882941
@synthesize baseURL = _baseURL;
883942
@synthesize attributes = _attributes;
884943
@synthesize originalOpacityAttributes = _originalOpacityAttributes;
944+
@synthesize src = _src;
945+
@synthesize videoId = _videoId;
946+
@synthesize launchedAppBundleIdentifier = _launchedAppBundleIdentifier;
885947

886948
@end

0 commit comments

Comments
 (0)