7 Module implementing the manager for GreaseMonkey scripts. |
7 Module implementing the manager for GreaseMonkey scripts. |
8 """ |
8 """ |
9 |
9 |
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, \ |
13 QByteArray |
13 QUrl, QByteArray |
14 from PyQt4.QtNetwork import QNetworkAccessManager |
14 from PyQt4.QtNetwork import QNetworkAccessManager |
15 |
15 |
16 import Utilities |
16 import Utilities |
17 import Preferences |
17 import Preferences |
18 |
18 |
42 """ |
42 """ |
43 Public method to show the configuration dialog. |
43 Public method to show the configuration dialog. |
44 |
44 |
45 @param parent reference to the parent widget (QWidget) |
45 @param parent reference to the parent widget (QWidget) |
46 """ |
46 """ |
47 from .GreaseMonkeyConfiguration.GreaseMonkeyConfigurationDialog import \ |
47 from .GreaseMonkeyConfiguration.GreaseMonkeyConfigurationDialog \ |
48 GreaseMonkeyConfigurationDialog |
48 import GreaseMonkeyConfigurationDialog |
49 self.__configDiaolg = GreaseMonkeyConfigurationDialog(self, parent) |
49 self.__configDiaolg = GreaseMonkeyConfigurationDialog(self, parent) |
50 self.__configDiaolg.show() |
50 self.__configDiaolg.show() |
51 |
51 |
52 def downloadScript(self, request): |
52 def downloadScript(self, request): |
53 """ |
53 """ |
74 """ |
74 """ |
75 Public method to get the path of the scripts directory. |
75 Public method to get the path of the scripts directory. |
76 |
76 |
77 @return path of the scripts directory (string) |
77 @return path of the scripts directory (string) |
78 """ |
78 """ |
79 return os.path.join(Utilities.getConfigDir(), "browser", "greasemonkey") |
79 return os.path.join( |
|
80 Utilities.getConfigDir(), "browser", "greasemonkey") |
80 |
81 |
81 def requireScriptsDirectory(self): |
82 def requireScriptsDirectory(self): |
82 """ |
83 """ |
83 Public method to get the path of the scripts directory. |
84 Public method to get the path of the scripts directory. |
84 |
85 |
97 if not requiresDir.exists() or len(urlList) == 0: |
98 if not requiresDir.exists() or len(urlList) == 0: |
98 return "" |
99 return "" |
99 |
100 |
100 script = "" |
101 script = "" |
101 |
102 |
102 settings = QSettings(os.path.join(self.requireScriptsDirectory(), "requires.ini"), |
103 settings = QSettings( |
103 QSettings.IniFormat) |
104 os.path.join(self.requireScriptsDirectory(), "requires.ini"), |
|
105 QSettings.IniFormat) |
104 settings.beginGroup("Files") |
106 settings.beginGroup("Files") |
105 for url in urlList: |
107 for url in urlList: |
106 if settings.contains(url): |
108 if settings.contains(url): |
107 fileName = settings.value(url) |
109 fileName = settings.value(url) |
108 try: |
110 try: |
117 |
119 |
118 def saveConfiguration(self): |
120 def saveConfiguration(self): |
119 """ |
121 """ |
120 Public method to save the configuration. |
122 Public method to save the configuration. |
121 """ |
123 """ |
122 Preferences.setHelp("GreaseMonkeyDisabledScripts", self.__disabledScripts) |
124 Preferences.setHelp("GreaseMonkeyDisabledScripts", |
|
125 self.__disabledScripts) |
123 |
126 |
124 def allScripts(self): |
127 def allScripts(self): |
125 """ |
128 """ |
126 Public method to get a list of all scripts. |
129 Public method to get a list of all scripts. |
127 |
130 |
259 scriptsDir.mkpath(self.scriptsDirectory()) |
262 scriptsDir.mkpath(self.scriptsDirectory()) |
260 |
263 |
261 if not scriptsDir.exists("requires"): |
264 if not scriptsDir.exists("requires"): |
262 scriptsDir.mkdir("requires") |
265 scriptsDir.mkdir("requires") |
263 |
266 |
264 self.__disabledScripts = Preferences.getHelp("GreaseMonkeyDisabledScripts") |
267 self.__disabledScripts = \ |
|
268 Preferences.getHelp("GreaseMonkeyDisabledScripts") |
265 |
269 |
266 from .GreaseMonkeyScript import GreaseMonkeyScript |
270 from .GreaseMonkeyScript import GreaseMonkeyScript |
267 for fileName in scriptsDir.entryList(["*.js"], QDir.Files): |
271 for fileName in scriptsDir.entryList(["*.js"], QDir.Files): |
268 absolutePath = scriptsDir.absoluteFilePath(fileName) |
272 absolutePath = scriptsDir.absoluteFilePath(fileName) |
269 script = GreaseMonkeyScript(self, absolutePath) |
273 script = GreaseMonkeyScript(self, absolutePath) |
280 """ |
284 """ |
281 Public method to allow the GreaseMonkey manager to connect to the page. |
285 Public method to allow the GreaseMonkey manager to connect to the page. |
282 |
286 |
283 @param page reference to the web page (HelpWebPage) |
287 @param page reference to the web page (HelpWebPage) |
284 """ |
288 """ |
285 page.mainFrame().javaScriptWindowObjectCleared.connect(self.pageLoadStarted) |
289 page.mainFrame().javaScriptWindowObjectCleared.connect( |
|
290 self.pageLoadStarted) |
286 |
291 |
287 def createRequest(self, op, request, outgoingData=None): |
292 def createRequest(self, op, request, outgoingData=None): |
288 """ |
293 """ |
289 Public method to create a request. |
294 Public method to create a request. |
290 |
295 |
291 @param op the operation to be performed (QNetworkAccessManager.Operation) |
296 @param op the operation to be performed |
|
297 (QNetworkAccessManager.Operation) |
292 @param request reference to the request object (QNetworkRequest) |
298 @param request reference to the request object (QNetworkRequest) |
293 @param outgoingData reference to an IODevice containing data to be sent |
299 @param outgoingData reference to an IODevice containing data to be sent |
294 (QIODevice) |
300 (QIODevice) |
295 @return reference to the created reply object (QNetworkReply) |
301 @return reference to the created reply object (QNetworkReply) |
296 """ |
302 """ |
297 if op == QNetworkAccessManager.GetOperation and \ |
303 if op == QNetworkAccessManager.GetOperation and \ |
298 request.rawHeader("X-Eric5-UserLoadAction") == QByteArray("1"): |
304 request.rawHeader("X-Eric5-UserLoadAction") == QByteArray("1"): |
299 urlString = request.url().toString(QUrl.RemoveFragment | QUrl.RemoveQuery) |
305 urlString = request.url().toString( |
|
306 QUrl.RemoveFragment | QUrl.RemoveQuery) |
300 if urlString.endswith(".user.js"): |
307 if urlString.endswith(".user.js"): |
301 self.downloadScript(request) |
308 self.downloadScript(request) |
302 from Helpviewer.Network.EmptyNetworkReply import EmptyNetworkReply |
309 from Helpviewer.Network.EmptyNetworkReply import \ |
|
310 EmptyNetworkReply |
303 return EmptyNetworkReply(self) |
311 return EmptyNetworkReply(self) |
304 |
312 |
305 return None |
313 return None |