-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCMController.m
618 lines (514 loc) · 17.5 KB
/
CMController.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*
* Copyright (c) 2006-2007 KATO Kazuyoshi <[email protected]>
* This source code is released under the MIT license.
*/
#import "CMController.h"
#import <WebKit/WebKit.h>
#import "GKLoader.h"
#import "GKAppsController.h"
#import "CMUserScript.h"
#import "Utils.h"
#import "config.h"
#if 0
# define DEBUG_LOG(...) NSLog(__VA_ARGS__)
#else
# define DEBUG_LOG ;
#endif
static NSString* BUNDLE_IDENTIFIER = @"info.8-p.GreaseKit";
static NSString* CONFIG_PATH = @"~/Library/Application Support/GreaseKit/config.xml";
static NSString* SCRIPT_DIR_PATH = @"~/Library/Application Support/GreaseKit/";
static NSString* CM_SCRIPT_DIR_PATH = @"~/Library/Application Support/Creammonkey/";
static NSString* CM_BUNDLE_PATH = @"~/Library/InputManagers/Creammonkey/";
static NSString* GK_INPUT_MANAGER_PATH = @"~/Library/InputManagers/GreaseKit/";
@implementation CMController
- (NSString*) loadScriptTemplate
{
NSBundle* bundle = [NSBundle bundleWithIdentifier: BUNDLE_IDENTIFIER];
NSString* path = [NSString stringWithFormat: @"%@/template.js", [bundle resourcePath]];
NSError* error;
return [NSString stringWithContentsOfFile: path
encoding: NSUTF8StringEncoding
error: &error];
}
- (NSArray*) scripts
{
return scripts_;
}
- (NSDictionary*) scriptElementsDictionary
{
NSMutableDictionary* result = [NSMutableDictionary dictionary];
NSString* path = [CONFIG_PATH stringByExpandingTildeInPath];
NSData* data = [NSData dataWithContentsOfFile: path];
if (! data) {
return result;
}
NSXMLDocument* doc;
doc = [[NSXMLDocument alloc] initWithData: data
options: 0
error: nil];
NSArray* ary = [[doc rootElement] elementsForName: @"Script"];
size_t i;
for (i = 0; i < [ary count]; i++) {
NSXMLElement* script = [ary objectAtIndex: i];
[result setObject: script
forKey: ElementAttribute(script, @"filename")];
}
[doc release];
return result;
}
- (void) saveScriptsConfig
{
NSXMLElement* root = [[NSXMLElement alloc] initWithName: @"UserScriptConfig"];
int i;
for (i = 0; i < [scripts_ count]; i++) {
CMUserScript* script = [scripts_ objectAtIndex: i];
[root addChild: [script XMLElement]];
}
NSString* path = [CONFIG_PATH stringByExpandingTildeInPath];
NSXMLDocument* doc;
doc = [[NSXMLDocument alloc] initWithRootElement: root];
[root release];
NSData* data = [doc XMLDataWithOptions: NSXMLNodePrettyPrint];
[doc release];
[data writeToFile: path atomically: YES];
}
- (void) addScript: (CMUserScript*) s
{
[s addObserver: self forKeyPath: @"enabled"
options: NSKeyValueObservingOptionNew
context: nil];
[scripts_ addObject: s];
}
- (void) installScript: (CMUserScript*) s
{
// write to local fs
[s install: scriptDir_];
// add
[self addScript: s];
[s release];
// enable and save config
[s setEnabled: YES];
[self saveScriptsConfig];
// reload all
[self reloadUserScripts: nil];
}
- (void) reloadMenu
{
int i;
int count = [topMenu indexOfItemWithTag: -1];
for (i = 0; i < count; i++) {
[topMenu removeItemAtIndex: 0];
}
for (i = 0; i < [scripts_ count]; i++) {
CMUserScript* script = [scripts_ objectAtIndex: i];
NSMenuItem* item = [[NSMenuItem alloc] init];
[item setTag: i];
[item setTarget: self];
[item setAction: @selector(toggleScriptEnable:)];
[item setState: [script isEnabled] ? NSOnState : NSOffState];
[item setTitle: [script name]];
[topMenu insertItem: item atIndex: i];
[item release];
}
}
- (NSArray*) scriptsAtDir: (NSString*) dir
{
NSDirectoryEnumerator* files;
files = [[NSFileManager defaultManager] enumeratorAtPath: dir];
// Should return nil instead of an empty array, otherwise -loadUserScripts
// will not create ~/Library/Appplication Support/GreaseKit directory for us
if (! files)
return nil;
NSMutableArray* result = [NSMutableArray array];
NSString* s;
while (s = [files nextObject]) {
NSString* path = [NSString stringWithFormat: @"%@/%@", dir, s];
if ([path hasSuffix: @".user.js"]) {
[result addObject: path];
}
}
return result;
}
- (void) showWarningAboutCreammonkey
{
NSString* path = [CM_BUNDLE_PATH stringByExpandingTildeInPath];
BOOL isDir;
[[NSFileManager defaultManager] fileExistsAtPath: path
isDirectory: &isDir];
if (! isDir) {
return;
}
NSAlert* alert = [[NSAlert alloc] init];
[alert setMessageText: @"Please remove Creammonkey"];
[alert setInformativeText: @"GreaseKit can't work with Creammonkey. Please remove Creammonkey from your InputManager folder and relaunch this application."];
[alert addButtonWithTitle: @"Ok"];
[[alert autorelease] runModal];
}
- (void) showWarningAboutGreaseKit_1_2
{
NSString* path = [GK_INPUT_MANAGER_PATH stringByExpandingTildeInPath];
BOOL isDir;
[[NSFileManager defaultManager] fileExistsAtPath: path
isDirectory: &isDir];
if (! isDir) {
return;
}
NSAlert* alert = [[NSAlert alloc] init];
[alert setMessageText: @"Please remove GreaseKit 1.2"];
[alert setInformativeText: @"GreaseKit 1.5 is implemented as a SIMBL plugin. Please remove GreaseKit 1.2 from your InputManager folder and relaunch this application."];
[alert addButtonWithTitle: @"Ok"];
[[alert autorelease] runModal];
}
- (void) installCreammonkeyScripts
{
NSString* dir = [CM_SCRIPT_DIR_PATH stringByExpandingTildeInPath];
NSArray* files = [self scriptsAtDir: dir];
if (! files)
return;
size_t i;
for (i = 0; i < [files count]; i++) {
NSString* path = [files objectAtIndex: i];
CMUserScript* s = [[CMUserScript alloc] initWithContentsOfFile: path
element: nil];
[self installScript: s];
}
}
- (void) loadUserScripts
{
NSFileManager* manager;
manager = [NSFileManager defaultManager];
NSString* dir = [SCRIPT_DIR_PATH stringByExpandingTildeInPath];
NSArray* files = [self scriptsAtDir: dir];
[self willChangeValueForKey: @"scripts"];
if (files) {
NSDictionary* config = [self scriptElementsDictionary];
size_t i;
for (i = 0; i < [files count]; i++) {
NSString* path;
path = [files objectAtIndex: i];
NSXMLElement* element = [config objectForKey: [path lastPathComponent]];
CMUserScript* script;
script = [[CMUserScript alloc] initWithContentsOfFile: path
element: element];
[self addScript: script];
[script release];
}
} else {
[manager createDirectoryAtPath: dir attributes: nil];
[self installCreammonkeyScripts];
}
[self didChangeValueForKey: @"scripts"];
[self reloadMenu];
}
- (void) observeValueForKeyPath: (NSString*) path
ofObject: (id) object
change: (NSDictionary*) change
context: (void*) context
{
if ([path isEqualTo: @"enabled"]) {
[self saveScriptsConfig];
[self reloadMenu];
}
}
- (NSArray*) matchedScripts: (NSURL*) url
{
if (! url)
return nil;
NSMutableArray* result = [[NSMutableArray alloc] init];
int i;
for (i = 0; i < [scripts_ count]; i++) {
CMUserScript* script = [scripts_ objectAtIndex: i];
if ([script isEnabled] && [script isMatched: url]) {
[result addObject: script];
}
}
return [result autorelease];
}
- (void) installAlertDidEnd: (NSAlert*) alert
returnCode: (int) returnCode
contextInfo: (void*) contextInfo
{
CMUserScript* script = (CMUserScript*) contextInfo;
if (returnCode == NSAlertFirstButtonReturn) {
[self installScript: script];
} else {
[script release];
}
}
- (void) showInstallAlertSheet: (CMUserScript*) script
webView: (WebView*) webView
{
NSAlert* alert = [[NSAlert alloc] init];
// Informative Text
NSMutableString* text = [[NSMutableString alloc] init];
if ([script name] && [script scriptDescription]) {
[text appendFormat: @"%@ - %@", [script name], [script scriptDescription]];
} else {
if ([script name])
[text appendString: [script name]];
else if ([script scriptDescription])
[text appendString: [script scriptDescription]];
}
[alert setInformativeText: text];
[text release];
// Message and Buttons
if([script isInstalled: scriptDir_]) {
[alert setMessageText: @"This script is installed, does it override the script?"];
[alert addButtonWithTitle: @"Override"];
} else {
[alert setMessageText: @"Install this script?"];
[alert addButtonWithTitle: @"Install"];
}
[alert addButtonWithTitle: @"Cancel"];
// Begin Sheet
[alert beginSheetModalForWindow: [webView window]
modalDelegate: self
didEndSelector: @selector(installAlertDidEnd:returnCode:contextInfo:)
contextInfo: script];
}
- (void) evalScriptsInFrame: (WebFrame*) frame
force: (BOOL) force
{
int i;
NSArray* children = [frame childFrames];
for (i = 0; i < [children count]; i++) {
[self evalScriptsInFrame: [children objectAtIndex: i]
force: force];
}
NSURL* url = WebFrameRequestURL(frame);
if (url && (! [[url scheme] isEqualToString: @"about"]) &&
[frame DOMDocument]) {
;
} else {
return;
}
// Eval!
WebScriptObject* scriptObject = [[frame webView] windowScriptObject];
WebScriptObject* func;
id result;
result = JSValueForKey([frame DOMDocument], @"readyState");
if (force || [result isEqualToString: @"loaded"]) {
id body = JSValueForKey([frame DOMDocument], @"body");
if ([(NSString*) JSValueForKey(body, @"__creammonkeyed__") intValue]) {
return;
} else {
[body setValue: [NSNumber numberWithBool: YES]
forKey: @"__creammonkeyed__"];
}
} else {
return;
}
DEBUG_LOG(@"eval: %d %@ %@ %d",
force, url, [frame DOMDocument],
[[self matchedScripts: url] count]);
NSArray* ary = [self matchedScripts: url];
for (i = 0; i < [ary count]; i++) {
CMUserScript* s = [ary objectAtIndex: i];
NSMutableString* ms = [NSMutableString stringWithFormat: scriptTemplate_, [s script]];
func = [scriptObject evaluateWebScript: ms];
NSArray* args = [NSArray arrayWithObjects: [frame DOMDocument], nil];
// eval on frame
JSFunctionCall(func, args);
}
}
- (void) progressStarted: (NSNotification*) n
{
// DEBUG_LOG(@"CMController %@ - progressStarted: %@", self, n);
WebView* webView = [n object];
WebDataSource* source = [[webView mainFrame] provisionalDataSource];
if (! source) {
// source = [[webView mainFrame] provisionalDataSource];
}
NSURL* url = [[source request] URL];
DEBUG_LOG(@"url = %@, matchedScripts = %d",
url, [[self matchedScripts: url] count]);
if ([[self matchedScripts: url] count] == 0) {
return;
}
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center addObserver: self
selector: @selector(progressChanged:)
name: WebViewProgressEstimateChangedNotification
object: [n object]];
}
- (void) progressChanged: (NSNotification*) n
{
// DEBUG_LOG(@"CMController %p - progressChanged: %@", self, n);
WebView* webView = [n object];
[self evalScriptsInFrame: [webView mainFrame]
force: NO];
}
- (void) progressFinished: (NSNotification*) n
{
WebView* webView = [n object];
NSURL* url = WebFrameRequestURL([webView mainFrame]);
[self evalScriptsInFrame: [webView mainFrame] force: YES];
// User Script
if ([[url absoluteString] hasSuffix: @".user.js"]) {
NSString* s = StringWithContentsOfURL(url);
CMUserScript* script;
script = [[CMUserScript alloc] initWithString: s
element: nil];
if (! [script name]) {
[script setName: [[url path] lastPathComponent]];
}
if (script) {
[self showInstallAlertSheet: script webView: webView];
}
}
}
#pragma mark Action
- (IBAction) toggleScriptEnable: (id) sender
{
CMUserScript* script = [scripts_ objectAtIndex: [sender tag]];
[script setEnabled: [sender state] != NSOnState];
#if 0
[sender setState: [script isEnabled] ? NSOnState : NSOffState];
[self saveScriptsConfig];
#endif
}
- (IBAction) editSelected: (id) sender
{
CMUserScript* script = [[scriptsController selectedObjects] objectAtIndex: 0];
[[NSWorkspace sharedWorkspace] openFile: [script filename]];
}
- (IBAction) uninstallSelected: (id) sender
{
CMUserScript* script = [[scriptsController selectedObjects] objectAtIndex: 0];
[script uninstall];
[self reloadUserScripts: sender];
}
- (IBAction) orderFrontAppsPanel: (id) sender
{
[[appsController_ window] makeKeyAndOrderFront: nil];
}
- (IBAction) orderFrontAboutPanel: (id) sender
{
NSImage* icon = [[NSWorkspace sharedWorkspace] iconForFileType: @"bundle"];
[icon setSize: NSMakeSize(128, 128)];
NSString* s = [[NSString alloc] initWithFormat: @"Version %s", VERSION];
NSDictionary* options;
options = [NSDictionary dictionaryWithObjectsAndKeys:
@"GreaseKit", @"ApplicationName",
icon, @"ApplicationIcon",
@"", @"Version",
s, @"ApplicationVersion",
@"Copyright (c) 2006-2009 KATO Kazuyoshi", @"Copyright",
nil];
[s release];
[NSApp orderFrontStandardAboutPanelWithOptions: options];
}
- (IBAction) reloadUserScripts: (id) sender
{
// FIXME: dirty?
int i;
for (i = 0; i < [scripts_ count]; i++) {
CMUserScript* s = [scripts_ objectAtIndex: i];
[s removeObserver: self forKeyPath: @"enabled"];
}
[scripts_ release];
scripts_ = [[NSMutableArray alloc] init];
[self loadUserScripts];
}
- (id) initWithApplications: (NSArray*) apps
{
self = [self init];
if (! self)
return nil;
appsController_ = [[GKAppsController alloc] initWithApplications: apps];
return self;
}
#pragma mark Override
- (id) init
{
DEBUG_LOG(@"CMController %p - init", self);
self = [super init];
if (! self)
return nil;
scriptDir_ = [[SCRIPT_DIR_PATH stringByExpandingTildeInPath] retain];
scriptTemplate_ = [[self loadScriptTemplate] retain];
scripts_ = nil;
[NSBundle loadNibNamed: @"Menu.nib" owner: self];
return self;
}
- (void) dealloc
{
NSLog(@"CMController - dealloc");
[appsController_ release];
[scripts_ release];
[super dealloc];
}
- (int) menuInsertIndex
{
size_t n = [[NSApp mainMenu] numberOfItems];
NSMenu* right = [NSApp windowsMenu];
if (! right) {
right = [NSApp helpMenu];
if (! right) {
return n;
}
}
size_t i;
for (i = 1; i < n; i++) {
NSMenuItem* item = [[NSApp mainMenu] itemAtIndex: i];
if ([item submenu] == right) {
return i;
}
}
return n;
}
- (void) awakeFromNib
{
[self reloadUserScripts: nil];
NSMenuItem* item = [[NSMenuItem alloc] init];
[[NSApp mainMenu] insertItem: item
atIndex: [self menuInsertIndex]];
[item release];
[topMenu setTitle: @"GreaseKit"];
[item setSubmenu: topMenu];
// Notification
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center addObserver: self
selector: @selector(progressStarted:)
name: WebViewProgressStartedNotification
object: nil];
[center addObserver: self
selector: @selector(progressFinished:)
name: WebViewProgressFinishedNotification
object: nil];
[center addObserver: self
selector: @selector(applicationDidFinishLaunching:)
name: NSApplicationDidFinishLaunchingNotification
object: NSApp];
}
- (void) windowWillClose: (NSNotification*) n
{
[self saveScriptsConfig];
}
- (void) applicationDidFinishLaunching: (NSNotification*) n
{
[self showWarningAboutCreammonkey];
[self showWarningAboutGreaseKit_1_2];
}
@end
// Workaround for PAC (Proxy Auto Config) by hetima-san <http://hetima.com>
@implementation DOMHTMLBodyElement(Info8_pCMUndefinedKeySupport)
- (id) valueForUndefinedKey: (NSString*) key
{
// NSLog(@"DOMHTMLBodyElement %p valueForUndefinedKey: %@", self, key);
if ([key isEqualToString:@"__creammonkeyed__"]) {
return nil;
}
return [super valueForUndefinedKey: key];
}
@end
@implementation DOMHTMLFrameSetElement(Info8_pCMUndefinedKeySupport)
- (id) valueForUndefinedKey: (NSString*) key
{
// NSLog(@"DOMHTMLFrameElement %p valueForUndefinedKey: %@", self, key);
if ([key isEqualToString:@"__creammonkeyed__"]) {
return nil;
}
return [super valueForUndefinedKey: key];
}
@end