-
Notifications
You must be signed in to change notification settings - Fork 78
/
WebSaverView.m
167 lines (135 loc) · 4.44 KB
/
WebSaverView.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
//
// WebSaverView.m
// WebSaver
//
// Created by Thomas Robinson on 10/13/09.
// Modified by Pekka Nikander in May 2012.
// Copyright (c) 2013, Thomas Robinson. All rights reserved.
// Copyright (c) 2012, Senseg. All rights reserved.
//
#import "WebSaverView.h"
#import <WebKit/WebKit.h>
#define REFRESH_DISABLED 0
#define REFRESH_SECONDS 1
#define REFRESH_MINUTES 2
#define REFRESH_HOURS 3
@implementation WebSaverView
static NSString * const ModuleName = @"com.github.tlrobinson.WebSaver";
static NSString * const URL_KEY = @"URL";
static NSString * const USERSCRIPT_KEY = @"UserScript";
static NSString * const REFRESH_INTERVAL_KEY = @"RefreshInterval";
static NSString * const REFRESH_UNITS_KEY = @"RefreshUnits";
static NSString * const DEFAULT_URL = @"https://github.com/tlrobinson/WebSaver";
static NSString * const DEFAULT_USERSCRIPT = @"document.body.style.backgroundColor = 'green';";
static double const DEFAULT_REFRESH_INTERVAL = 1.0;
static long const DEFAULT_REFRESH_UNITS = REFRESH_MINUTES;
- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview
{
self = [super initWithFrame:frame isPreview:isPreview];
if (self) {
refreshTimer = nil;
defaults = [ScreenSaverDefaults defaultsForModuleWithName:ModuleName];
[defaults registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
DEFAULT_URL, URL_KEY,
DEFAULT_USERSCRIPT, USERSCRIPT_KEY,
[NSNumber numberWithDouble:DEFAULT_REFRESH_INTERVAL], REFRESH_INTERVAL_KEY,
[NSNumber numberWithLong:DEFAULT_REFRESH_UNITS], REFRESH_UNITS_KEY,
nil]];
webView = [[WebView alloc] initWithFrame:[self bounds] frameName:nil groupName:nil];
[webView setFrameLoadDelegate:self];
[self addSubview:webView];
[self loadWebView];
}
return self;
}
- (BOOL)hasConfigureSheet
{
return YES;
}
- (NSString *)url
{
return [defaults valueForKey: URL_KEY];
}
- (long)refreshUnits
{
return [(NSNumber *)[defaults valueForKey:REFRESH_UNITS_KEY] longValue];
}
- (double)refreshInterval
{
return [(NSNumber *)[defaults valueForKey:REFRESH_INTERVAL_KEY] doubleValue];
}
- (NSString *)userScript
{
return [defaults valueForKey:USERSCRIPT_KEY];
}
- (NSWindow *)configureSheet
{
if (!configSheet)
{
if (![NSBundle loadNibNamed:@"ConfigureSheet" owner:self])
{
NSLog( @"Failed to load configure sheet." );
NSBeep();
}
}
[url setStringValue:[self url]];
[refreshInterval setDoubleValue:[self refreshInterval]];
[refreshUnits selectItemWithTag:[self refreshUnits]];
[[userScript textStorage] setAttributedString:[[NSAttributedString alloc] initWithString:[self userScript]]];
[self updatePanel];
return configSheet;
}
- (void)updatePanel
{
[refreshInterval setEnabled:[[refreshUnits selectedItem] tag] > 0];
}
- (void)refreshWebView:(NSTimer*)theTimer
{
[webView reload:nil];
}
- (void)loadWebView
{
[webView setMainFrameURL:[defaults valueForKey:URL_KEY]];
if (refreshTimer)
{
[refreshTimer invalidate];
[refreshTimer release];
refreshTimer = nil;
}
long units = [self refreshUnits];
NSTimeInterval interval = 0;
if (units == REFRESH_SECONDS)
interval = [self refreshInterval];
else if (units == REFRESH_MINUTES)
interval = 60 * [self refreshInterval];
else if (units == REFRESH_HOURS)
interval = 60 * 60 * [self refreshInterval];
if (interval > 0)
{
refreshTimer = [NSTimer scheduledTimerWithTimeInterval:interval target:self selector:@selector(refreshWebView:) userInfo:nil repeats:YES];
}
}
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
[sender stringByEvaluatingJavaScriptFromString:[defaults valueForKey:USERSCRIPT_KEY]];
}
// IBActions
- (IBAction) okClick:(id)sender
{
[defaults setValue:[url stringValue] forKey:URL_KEY];
[defaults setValue:[NSNumber numberWithDouble:[refreshInterval doubleValue]] forKey:REFRESH_INTERVAL_KEY];
[defaults setValue:[NSNumber numberWithLong:[[refreshUnits selectedItem] tag]] forKey:REFRESH_UNITS_KEY];
[defaults setValue:[[userScript textStorage] string] forKey:USERSCRIPT_KEY];
[defaults synchronize];
[[NSApplication sharedApplication] endSheet:configSheet];
[self loadWebView];
}
- (IBAction)cancelClick:(id)sender
{
[[NSApplication sharedApplication] endSheet:configSheet];
}
- (IBAction)changeRefreshUnits:(id)sender
{
[self updatePanel];
}
@end