This repository has been archived by the owner on Oct 18, 2018. It is now read-only.
/
KeywurlPlugin.m
140 lines (125 loc) · 4.33 KB
/
KeywurlPlugin.m
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#import "KeywurlPlugin.h"
#import "KeywurlBrowserWebView.h"
#import "KeywurlBrowserWindowController.h"
#import "IntroWindowController.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];
#ifdef __OBJC2__
[KeywurlBrowserWindowController keywurl_load];
[KeywurlBrowserWebView keywurl_load];
#else
NSClassFromString(@"BrowserWindowController");
[[KeywurlBrowserWindowController class] poseAsClass: [BrowserWindowController class]];
NSClassFromString(@"BrowserWebView");
[[KeywurlBrowserWebView class] poseAsClass: [BrowserWebView class]];
#endif
NSUserDefaults* preferences = [[NSUserDefaults standardUserDefaults] retain];
[preferences setObject: @"world" forKey: @"hello"];
[preferences release];
if ([IntroWindowController shouldShow]) {
IntroWindowController* introController = [[IntroWindowController alloc] init];
[introController show];
}
}
+ (KeywurlPlugin*) sharedInstance {
if (plugin == nil) {
plugin = [[KeywurlPlugin alloc] init];
}
return plugin;
}
- (id) init {
fKeywordMapper = [[KeywordMapper alloc] init];
fLoaded = YES;
introWindow = nil;
return self;
}
- (void) dealloc {
if (preferences) {
[preferences release];
}
[super dealloc];
}
- (NSString*) preferenceFileName {
NSArray* paths = NSSearchPathForDirectoriesInDomains(
NSApplicationSupportDirectory, NSUserDomainMask, YES);
return [[paths objectAtIndex: 0] stringByAppendingPathComponent: @"Keywurl/Settings.plist"];
}
- (NSMutableDictionary*) preferences {
if (!preferences) {
NSDictionary* immutablePreferences = [NSDictionary dictionaryWithContentsOfFile: [self preferenceFileName]];
if (immutablePreferences) {
preferences = [immutablePreferences mutableCopy];
} else {
preferences = [NSMutableDictionary dictionary];
}
}
return preferences;
}
- (void) savePreferences {
if (preferences) {
[preferences writeToFile: [self preferenceFileName] atomically: YES];
}
}
- (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) {
if ([node nodeType] == DOM_ELEMENT_NODE) {
DOMElement* element = (DOMElement*) node;
if ([[element tagName] isEqualToString: @"FORM"]) {
formElement = element;
break;
}
}
node = [node parentNode];
}
}
if (inputElement && formElement) {
[[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