|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the GreaseMonkey script. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import QUrl, QRegExp |
|
11 |
|
12 from .GreaseMonkeyUrlMatcher import GreaseMonkeyUrlMatcher |
|
13 |
|
14 |
|
15 class GreaseMonkeyScript(object): |
|
16 """ |
|
17 Class implementing the GreaseMonkey script. |
|
18 """ |
|
19 DocumentStart = 0 |
|
20 DocumentEnd = 1 |
|
21 |
|
22 def __init__(self, manager, path): |
|
23 """ |
|
24 Constructor |
|
25 |
|
26 @param manager reference to the manager object (GreaseMonkeyManager) |
|
27 @param path path of the Javascript file (string) |
|
28 """ |
|
29 self.__manager = manager |
|
30 |
|
31 self.__name = "" |
|
32 self.__namespace = "GreaseMonkeyNS" |
|
33 self.__description = "" |
|
34 self.__version = "" |
|
35 |
|
36 self.__include = [] |
|
37 self.__exclude = [] |
|
38 |
|
39 self.__downloadUrl = QUrl() |
|
40 self.__startAt = GreaseMonkeyScript.DocumentEnd |
|
41 |
|
42 self.__script = "" |
|
43 self.__fileName = path |
|
44 self.__enabled = True |
|
45 self.__valid = False |
|
46 |
|
47 self.__parseScript(path) |
|
48 |
|
49 def isValid(self): |
|
50 """ |
|
51 Public method to check the validity of the script. |
|
52 |
|
53 @return flag indicating a valid script (boolean) |
|
54 """ |
|
55 return self.__valid |
|
56 |
|
57 def name(self): |
|
58 """ |
|
59 Public method to get the name of the script. |
|
60 |
|
61 @return name of the script (string) |
|
62 """ |
|
63 return self.__name |
|
64 |
|
65 def nameSpace(self): |
|
66 """ |
|
67 Public method to get the name space of the script. |
|
68 |
|
69 @return name space of the script (string) |
|
70 """ |
|
71 return self.__namespace |
|
72 |
|
73 def fullName(self): |
|
74 """ |
|
75 Public method to get the full name of the script. |
|
76 |
|
77 @return full name of the script (string) |
|
78 """ |
|
79 return "{0}/{1}".format(self.__namespace, self.__name) |
|
80 |
|
81 def description(self): |
|
82 """ |
|
83 Public method to get the description of the script. |
|
84 |
|
85 @return description of the script (string) |
|
86 """ |
|
87 return self.__description |
|
88 |
|
89 def version(self): |
|
90 """ |
|
91 Public method to get the version of the script. |
|
92 |
|
93 @return version of the script (string) |
|
94 """ |
|
95 return self.__version |
|
96 |
|
97 def downloadUrl(self): |
|
98 """ |
|
99 Public method to get the download URL of the script. |
|
100 |
|
101 @return download URL of the script (QUrl) |
|
102 """ |
|
103 return QUrl(self.__downloadUrl) |
|
104 |
|
105 def startAt(self): |
|
106 """ |
|
107 Public method to get the start point of the script. |
|
108 |
|
109 @return start point of the script (DocumentStart or DocumentEnd) |
|
110 """ |
|
111 return self.__startAt |
|
112 |
|
113 def isEnabled(self): |
|
114 """ |
|
115 Public method to check, if the script is enabled. |
|
116 |
|
117 @return flag indicating an enabled state (boolean) |
|
118 """ |
|
119 return self.__enabled |
|
120 |
|
121 def setEnabled(self, enable): |
|
122 """ |
|
123 Public method to enable a script. |
|
124 |
|
125 @param enable flag indicating the new enabled state (boolean) |
|
126 """ |
|
127 self.__enabled = enable |
|
128 |
|
129 def include(self): |
|
130 """ |
|
131 Public method to get the list of included URLs. |
|
132 |
|
133 @return list of included URLs (list of strings) |
|
134 """ |
|
135 list = [] |
|
136 for matcher in self.__include: |
|
137 list.append(matcher.pattern()) |
|
138 return list |
|
139 |
|
140 def exclude(self): |
|
141 """ |
|
142 Public method to get the list of excluded URLs. |
|
143 |
|
144 @return list of excluded URLs (list of strings) |
|
145 """ |
|
146 list = [] |
|
147 for matcher in self.__exclude: |
|
148 list.append(matcher.pattern()) |
|
149 return list |
|
150 |
|
151 def script(self): |
|
152 """ |
|
153 Public method to get the Javascript source. |
|
154 |
|
155 @return Javascript source (string) |
|
156 """ |
|
157 return self.__script |
|
158 |
|
159 def fileName(self): |
|
160 """ |
|
161 Public method to get the path of the Javascript file. |
|
162 |
|
163 @return path path of the Javascript file (string) |
|
164 """ |
|
165 return self.__fileName |
|
166 |
|
167 def match(self, urlString): |
|
168 """ |
|
169 Public method to check, if the script matches the given URL. |
|
170 |
|
171 @return flag indicating a match (boolean) |
|
172 """ |
|
173 if not self.__enabled: |
|
174 return False |
|
175 |
|
176 for matcher in self.__exclude: |
|
177 if matcher.match(urlString): |
|
178 return False |
|
179 |
|
180 for matcher in self.__include: |
|
181 if matcher.match(urlString): |
|
182 return True |
|
183 |
|
184 return False |
|
185 |
|
186 def __parseScript(self, path): |
|
187 """ |
|
188 Private method to parse the given script and populate the data structure. |
|
189 |
|
190 @param path path of the Javascript file (string) |
|
191 """ |
|
192 try: |
|
193 f = open(path) |
|
194 fileData = f.read() |
|
195 f.close() |
|
196 except (IOError, OSError): |
|
197 # silently ignore because it shouldn't happen |
|
198 return |
|
199 |
|
200 rx = QRegExp("// ==UserScript==(.*)// ==/UserScript==") |
|
201 rx.indexIn(fileData) |
|
202 metaDataBlock = rx.cap(1).strip() |
|
203 |
|
204 if metaDataBlock == "": |
|
205 # invalid script file |
|
206 return |
|
207 |
|
208 requireList = [] |
|
209 for line in metaDataBlock.splitlines(): |
|
210 if not line.startswith("// @"): |
|
211 continue |
|
212 |
|
213 line = line[3:].replace("\t", " ") |
|
214 index = line.find(" ") |
|
215 if index < 0: |
|
216 continue |
|
217 |
|
218 key = line[:index].strip() |
|
219 value = line[index + 1:].strip() |
|
220 |
|
221 # Ignored values: @resource, @unwrap |
|
222 |
|
223 if not key or not value: |
|
224 continue |
|
225 |
|
226 if key == "@name": |
|
227 self.__name = value |
|
228 |
|
229 elif key == "@namespace": |
|
230 self.__namespace = value |
|
231 |
|
232 elif key == "@description": |
|
233 self.__description = value |
|
234 |
|
235 elif key == "@version": |
|
236 self.__version = value |
|
237 |
|
238 elif key == "@updateURL": |
|
239 self.__downloadUrl = QUrl(value) |
|
240 |
|
241 elif key in ["@include", "@match"]: |
|
242 self.__include.append(GreaseMonkeyUrlMatcher(value)) |
|
243 |
|
244 elif key in ["@exclude", "@exclude_match"]: |
|
245 self.__exclude.append(GreaseMonkeyUrlMatcher(value)) |
|
246 |
|
247 elif key == "@require": |
|
248 requireList.append(value) |
|
249 |
|
250 elif key == "@run-at": |
|
251 if value == "document-end": |
|
252 self.__startAt = GreaseMonkeyScript.DocumentEnd |
|
253 elif value == "document-start": |
|
254 self.__startAt = GreaseMonkeyScript.DocumentStart |
|
255 |
|
256 elif key == "@downloadURL" and self.__downloadUrl.isEmpty(): |
|
257 self.__downloadUrl = QUrl(value) |
|
258 |
|
259 if not self.__include: |
|
260 self.__include.append(GreaseMonkeyUrlMatcher("*")) |
|
261 |
|
262 marker = "// ==/UserScript==" |
|
263 index = fileData.find(marker) + len(marker) |
|
264 script = fileData[index:].strip() |
|
265 script = "{0}{1}".format( |
|
266 self.__manager.requireScripts(requireList), |
|
267 script) |
|
268 self.__script = "(function(){{{0}}})();".format(script) |
|
269 self.__valid = len(script) > 0 |