Helpviewer/GreaseMonkey/GreaseMonkeyManager.py

changeset 2403
e3d7a861547c
parent 2302
f29e9405c851
child 2525
8b507a9a2d40
child 3002
6ffc581f00f1
equal deleted inserted replaced
2402:304fcefd3b04 2403:e3d7a861547c
10 import os 10 import os
11 11
12 from PyQt4.QtCore import pyqtSignal, QObject, QTimer, QFile, QDir, QSettings, QUrl, \ 12 from PyQt4.QtCore import pyqtSignal, QObject, QTimer, QFile, QDir, QSettings, QUrl, \
13 QByteArray 13 QByteArray
14 from PyQt4.QtNetwork import QNetworkAccessManager 14 from PyQt4.QtNetwork import QNetworkAccessManager
15
16 from .GreaseMonkeyJavaScript import bootstrap_js
17 from .GreaseMonkeyDownloader import GreaseMonkeyDownloader
18 from .GreaseMonkeyScript import GreaseMonkeyScript
19
20 from .GreaseMonkeyConfiguration.GreaseMonkeyConfigurationDialog import \
21 GreaseMonkeyConfigurationDialog
22
23 from Helpviewer.Network.EmptyNetworkReply import EmptyNetworkReply
24 15
25 import Utilities 16 import Utilities
26 import Preferences 17 import Preferences
27 18
28 19
51 """ 42 """
52 Public method to show the configuration dialog. 43 Public method to show the configuration dialog.
53 44
54 @param parent reference to the parent widget (QWidget) 45 @param parent reference to the parent widget (QWidget)
55 """ 46 """
47 from .GreaseMonkeyConfiguration.GreaseMonkeyConfigurationDialog import \
48 GreaseMonkeyConfigurationDialog
56 self.__configDiaolg = GreaseMonkeyConfigurationDialog(self, parent) 49 self.__configDiaolg = GreaseMonkeyConfigurationDialog(self, parent)
57 self.__configDiaolg.show() 50 self.__configDiaolg.show()
58 51
59 def downloadScript(self, request): 52 def downloadScript(self, request):
60 """ 53 """
61 Public method to download a GreaseMonkey script. 54 Public method to download a GreaseMonkey script.
62 55
63 @param request reference to the request (QNetworkRequest) 56 @param request reference to the request (QNetworkRequest)
64 """ 57 """
58 from .GreaseMonkeyDownloader import GreaseMonkeyDownloader
65 downloader = GreaseMonkeyDownloader(request, self) 59 downloader = GreaseMonkeyDownloader(request, self)
66 downloader.finished.connect(self.__downloaderFinished) 60 downloader.finished.connect(self.__downloaderFinished)
67 self.__downloaders.append(downloader) 61 self.__downloaders.append(downloader)
68 62
69 def __downloaderFinished(self): 63 def __downloaderFinished(self):
180 @return flag indicating success (boolean) 174 @return flag indicating success (boolean)
181 """ 175 """
182 if not script: 176 if not script:
183 return False 177 return False
184 178
179 from .GreaseMonkeyScript import GreaseMonkeyScript
185 if script.startAt() == GreaseMonkeyScript.DocumentStart: 180 if script.startAt() == GreaseMonkeyScript.DocumentStart:
186 self.__startScripts.append(script) 181 self.__startScripts.append(script)
187 else: 182 else:
188 self.__endScripts.append(script) 183 self.__endScripts.append(script)
189 184
198 @return flag indicating success (boolean) 193 @return flag indicating success (boolean)
199 """ 194 """
200 if not script: 195 if not script:
201 return False 196 return False
202 197
198 from .GreaseMonkeyScript import GreaseMonkeyScript
203 if script.startAt() == GreaseMonkeyScript.DocumentStart: 199 if script.startAt() == GreaseMonkeyScript.DocumentStart:
204 try: 200 try:
205 self.__startScripts.remove(script) 201 self.__startScripts.remove(script)
206 except ValueError: 202 except ValueError:
207 pass 203 pass
240 urlString = bytes(frame.url().toEncoded()).decode() 236 urlString = bytes(frame.url().toEncoded()).decode()
241 237
242 if not self.canRunOnScheme(urlScheme): 238 if not self.canRunOnScheme(urlScheme):
243 return 239 return
244 240
241 from .GreaseMonkeyJavaScript import bootstrap_js
245 for script in self.__startScripts: 242 for script in self.__startScripts:
246 if script.match(urlString): 243 if script.match(urlString):
247 frame.evaluateJavaScript(bootstrap_js + script.script()) 244 frame.evaluateJavaScript(bootstrap_js + script.script())
248 245
249 for script in self.__endScripts: 246 for script in self.__endScripts:
264 if not scriptsDir.exists("requires"): 261 if not scriptsDir.exists("requires"):
265 scriptsDir.mkdir("requires") 262 scriptsDir.mkdir("requires")
266 263
267 self.__disabledScripts = Preferences.getHelp("GreaseMonkeyDisabledScripts") 264 self.__disabledScripts = Preferences.getHelp("GreaseMonkeyDisabledScripts")
268 265
266 from .GreaseMonkeyScript import GreaseMonkeyScript
269 for fileName in scriptsDir.entryList(["*.js"], QDir.Files): 267 for fileName in scriptsDir.entryList(["*.js"], QDir.Files):
270 absolutePath = scriptsDir.absoluteFilePath(fileName) 268 absolutePath = scriptsDir.absoluteFilePath(fileName)
271 script = GreaseMonkeyScript(self, absolutePath) 269 script = GreaseMonkeyScript(self, absolutePath)
272 270
273 if script.fullName() in self.__disabledScripts: 271 if script.fullName() in self.__disabledScripts:
299 if op == QNetworkAccessManager.GetOperation and \ 297 if op == QNetworkAccessManager.GetOperation and \
300 request.rawHeader("X-Eric5-UserLoadAction") == QByteArray("1"): 298 request.rawHeader("X-Eric5-UserLoadAction") == QByteArray("1"):
301 urlString = request.url().toString(QUrl.RemoveFragment | QUrl.RemoveQuery) 299 urlString = request.url().toString(QUrl.RemoveFragment | QUrl.RemoveQuery)
302 if urlString.endswith(".user.js"): 300 if urlString.endswith(".user.js"):
303 self.downloadScript(request) 301 self.downloadScript(request)
302 from Helpviewer.Network.EmptyNetworkReply import EmptyNetworkReply
304 return EmptyNetworkReply(self) 303 return EmptyNetworkReply(self)
305 304
306 return None 305 return None

eric ide

mercurial