8 """ |
8 """ |
9 |
9 |
10 from __future__ import unicode_literals |
10 from __future__ import unicode_literals |
11 |
11 |
12 from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QUrl, QRegExp, \ |
12 from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QUrl, QRegExp, \ |
13 QByteArray, QCryptographicHash, qVersion |
13 QByteArray, QCryptographicHash |
14 from PyQt5.QtWebEngineWidgets import QWebEngineScript |
14 from PyQt5.QtWebEngineWidgets import QWebEngineScript |
15 |
15 |
16 from .GreaseMonkeyJavaScript import bootstrap_js, values_js |
16 from .GreaseMonkeyJavaScript import bootstrap_js, values_js |
17 from .GreaseMonkeyDownloader import GreaseMonkeyDownloader |
17 from .GreaseMonkeyDownloader import GreaseMonkeyDownloader |
18 |
18 |
19 from ..Tools.DelayedFileWatcher import DelayedFileWatcher |
19 from ..Tools.DelayedFileWatcher import DelayedFileWatcher |
20 from ..WebBrowserPage import WebBrowserPage |
20 from ..WebBrowserPage import WebBrowserPage |
|
21 |
|
22 from Globals import qVersionTuple |
21 |
23 |
22 |
24 |
23 class GreaseMonkeyScript(QObject): |
25 class GreaseMonkeyScript(QObject): |
24 """ |
26 """ |
25 Class implementing the GreaseMonkey script. |
27 Class implementing the GreaseMonkey script. |
326 |
328 |
327 nspace = bytes(QCryptographicHash.hash( |
329 nspace = bytes(QCryptographicHash.hash( |
328 QByteArray(self.fullName().encode("utf-8")), |
330 QByteArray(self.fullName().encode("utf-8")), |
329 QCryptographicHash.Md4).toHex()).decode("ascii") |
331 QCryptographicHash.Md4).toHex()).decode("ascii") |
330 valuesScript = values_js.format(nspace) |
332 valuesScript = values_js.format(nspace) |
331 if qVersion() < "5.8.0": |
333 if qVersionTuple() < (5, 8, 0): |
332 runCheck = """ |
334 runCheck = """ |
333 for (var value of {0}) {{ |
335 for (var value of {0}) {{ |
334 var re = new RegExp(value); |
336 var re = new RegExp(value); |
335 if (re.test(window.location.href)) {{ |
337 if (re.test(window.location.href)) {{ |
336 return; |
338 return; |
368 |
370 |
369 @return prepared script object |
371 @return prepared script object |
370 @rtype QWebEngineScript |
372 @rtype QWebEngineScript |
371 @exception ValueError raised to indicate an unsupported start point |
373 @exception ValueError raised to indicate an unsupported start point |
372 """ |
374 """ |
373 if qVersion() < "5.8.0": |
375 if qVersionTuple() < (5, 8, 0): |
374 if self.startAt() == GreaseMonkeyScript.DocumentStart: |
376 if self.startAt() == GreaseMonkeyScript.DocumentStart: |
375 injectionPoint = QWebEngineScript.DocumentCreation |
377 injectionPoint = QWebEngineScript.DocumentCreation |
376 elif self.startAt() == GreaseMonkeyScript.DocumentEnd: |
378 elif self.startAt() == GreaseMonkeyScript.DocumentEnd: |
377 injectionPoint = QWebEngineScript.DocumentReady |
379 injectionPoint = QWebEngineScript.DocumentReady |
378 elif self.startAt() == GreaseMonkeyScript.DocumentIdle: |
380 elif self.startAt() == GreaseMonkeyScript.DocumentIdle: |
383 script = QWebEngineScript() |
385 script = QWebEngineScript() |
384 script.setSourceCode("{0}\n{1}".format( |
386 script.setSourceCode("{0}\n{1}".format( |
385 bootstrap_js, self.__script |
387 bootstrap_js, self.__script |
386 )) |
388 )) |
387 script.setName(self.fullName()) |
389 script.setName(self.fullName()) |
388 if qVersion() < "5.8.0": |
390 if qVersionTuple() < (5, 8, 0): |
389 script.setInjectionPoint(injectionPoint) |
391 script.setInjectionPoint(injectionPoint) |
390 script.setWorldId(WebBrowserPage.SafeJsWorld) |
392 script.setWorldId(WebBrowserPage.SafeJsWorld) |
391 script.setRunsOnSubFrames(not self.__noFrames) |
393 script.setRunsOnSubFrames(not self.__noFrames) |
392 return script |
394 return script |
393 |
395 |
399 @param patterns list of match patterns |
401 @param patterns list of match patterns |
400 @type list of str |
402 @type list of str |
401 @return JavaScript script containing the list |
403 @return JavaScript script containing the list |
402 @rtype str |
404 @rtype str |
403 """ |
405 """ |
404 if qVersion() >= "5.8.0": |
406 if qVersionTuple() >= (5, 8, 0): |
405 script = "" |
407 script = "" |
406 else: |
408 else: |
407 patternList = [] |
409 patternList = [] |
408 for pattern in patterns: |
410 for pattern in patterns: |
409 if pattern.startswith("/") and pattern.endswith("/") and \ |
411 if pattern.startswith("/") and pattern.endswith("/") and \ |