public
Description: Keywurl is a small extension for Safari that adds keyword search, similar to Firefox's "Quick Search" functionality.
Homepage: http://alexstaubo.github.com/keywurl/
Clone URL: git://github.com/alexstaubo/keywurl.git
keywurl / KeywurlBrowserWebView.m
100644 68 lines (59 sloc) 2.617 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "KeywurlBrowserWebView.h"
#include "KeywurlBrowserWindowController.h"
#include "KeywurlPlugin.h"
 
@implementation KeywurlBrowserWebView
 
#if KEYWURL_SAFARI_VERSION >= 4
 
- (NSArray*) fallbackURLs {
    // If Safari has failed to resolve the address into a proper host name, the
    // list of fallback URLs is empty and we can intercept the URL and map it
    NSArray* urls = [super fallbackURLs];
    if (urls == nil || [urls count] == 0) {
     KeywurlPlugin* plugin = [KeywurlPlugin sharedInstance];
        KeywordMapper* mapper = [plugin keywordMapper];
        LocationFieldEditor* fieldEditor = (LocationFieldEditor*) [
          (KeywurlBrowserWindowController*) [self windowController] keywurl_locationFieldEditor];
     NSString* address = [[fieldEditor textStorage] string];
        NSString* mapped = [mapper mapKeywordInput: address];
     if (mapped && ![mapped isEqualToString: address]) {
            NSURL* mappedUrl = [NSURL URLWithString: mapped];
            urls = [NSArray arrayWithObject: mappedUrl];
        }
    }
    return urls;
}
 
#else
 
- (NSArray*) fallbackURLs {
KeywurlPlugin* plugin = [KeywurlPlugin sharedInstance];
    KeywordMapper* mapper = [plugin keywordMapper];
    LocationFieldEditor* fieldEditor = (LocationFieldEditor*) [
      (KeywurlBrowserWindowController*) [self windowController] keywurl_locationFieldEditor];
NSString* address = [[fieldEditor textStorage] string];
    NSString* mapped = [mapper mapKeywordInput: address];
if (mapped && ![mapped isEqualToString: address]) {
        NSURL* mappedUrl = [NSURL URLWithString: mapped];
        return [NSArray arrayWithObject: mappedUrl];
    } else {
        return [super fallbackURLs];
    }
}
 
#endif
 
- (id) webView: (id) sender
    contextMenuItemsForElement: (id) elementDictionary
    defaultMenuItems: (NSMutableArray*) defaultMenuItems {
    NSString* documentUrl = [self mainFrameURL];
    KeywurlPlugin* plugin = [KeywurlPlugin sharedInstance];
    id node = [elementDictionary objectForKey: @"WebElementDOMNode"];
    if (node) {
        [defaultMenuItems addObject: [NSMenuItem separatorItem]];
        NSMenuItem* item = [[NSMenuItem alloc] initWithTitle: @"Create Keyword Search"
            action: @selector(createKeywordSearchFromForm:)
            keyEquivalent: @""];
        [item setRepresentedObject: [NSArray arrayWithObjects: documentUrl, node, nil]];
        [item setTarget: plugin];
        [defaultMenuItems addObject: item];
    }
    return [super webView: sender
         contextMenuItemsForElement: elementDictionary
         defaultMenuItems: defaultMenuItems];
}
 
@end