forked from atombender/keywurl
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKeywurlPlugin.m
More file actions
95 lines (86 loc) · 3.08 KB
/
Copy pathKeywurlPlugin.m
File metadata and controls
95 lines (86 loc) · 3.08 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#import "KeywurlPlugin.h"
#import "KeywurlBrowserWebView.h"
#import "KeywurlBrowserWindowController.h"
#import "Safari.h"
#import "KeywordSaveController.h"
#import "Constants.h"
static KeywurlPlugin* plugin = nil;
@implementation KeywurlPlugin
+ (void) load {
#ifdef KEYWURL_BETA_BUILD
NSLog(@"Keywurl version %d.%d.%d beta %d loading",
KEYWURL_MAJORVERSION, KEYWURL_MINORVERSION, KEYWURL_MAINTVERSION, KEYWURL_BETA_BUILD);
#else
NSLog(@"Keywurl version %d.%d.%d loading",
KEYWURL_MAJORVERSION, KEYWURL_MINORVERSION, KEYWURL_MAINTVERSION);
#endif
KeywurlPlugin* plugin = [KeywurlPlugin sharedInstance];
NSClassFromString(@"BrowserWindowController");
[[KeywurlBrowserWindowController class] poseAsClass: [BrowserWindowController class]];
NSClassFromString(@"BrowserWebView");
[[KeywurlBrowserWebView class] poseAsClass: [BrowserWebView class]];
}
+ (KeywurlPlugin*) sharedInstance {
if (plugin == nil) {
plugin = [[KeywurlPlugin alloc] init];
}
return plugin;
}
- (id) init {
fKeywordMapper = [[KeywordMapper alloc] init];
fLoaded = YES;
return self;
}
- (KeywordMapper*) keywordMapper {
return fKeywordMapper;
}
- (BOOL) isLoaded {
return fLoaded;
}
- (void) createKeywordSearchFromForm: (id) sender {
NSArray* parameters = (NSArray*) [sender representedObject];
NSString* documentUrl = (NSString*) [parameters objectAtIndex: 0];
DOMElement* inputElement = (DOMElement*) [parameters objectAtIndex: 1];
DOMElement* formElement = nil;
if ([inputElement respondsToSelector: @selector(form)]) {
formElement = [inputElement form];
}
if (!formElement) {
DOMNode* node = inputElement;
while (node) {
NSLog(@"looking at node: %@", node);
if ([node nodeType] == DOM_ELEMENT_NODE) {
DOMElement* element = (DOMElement*) node;
NSLog(@" element: %@", [element tagName]);
if ([[element tagName] isEqualToString: @"FORM"]) {
formElement = element;
break;
}
}
node = [node parentNode];
}
}
if (inputElement && formElement) {
KeywordSaveController* controller = [[KeywordSaveController alloc] initWithUrl: documentUrl
inputElement: inputElement
formElement: formElement];
} else {
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle: @"OK"];
[alert setMessageText: @"Could not create keyword"];
[alert setInformativeText: [NSString stringWithFormat:
@"The field you clicked on seem to be constructed in a way that Keywurl \
does not understand. \
\
For example, the field may be JavaScript/AJAX-based, or the HTML may be malformed. \
Keywurl can only deal with good old-fashioned HTML forms. \
\
Sorry."]];
[alert setAlertStyle: NSInformationalAlertStyle];
[alert beginSheetModalForWindow: [NSApp keyWindow]
modalDelegate: nil
didEndSelector: nil
contextInfo: nil];
}
}
@end