src/eric7/WebBrowser/GreaseMonkey/GreaseMonkeyScript.py

branch
eric7
changeset 10485
287a3ae95e00
parent 10475
ee41fab001f2
child 11090
f5f5f5803935
equal deleted inserted replaced
10484:ad7a6d699a0d 10485:287a3ae95e00
5 5
6 """ 6 """
7 Module implementing the GreaseMonkey script. 7 Module implementing the GreaseMonkey script.
8 """ 8 """
9 9
10 import enum
10 import re 11 import re
11 12
12 from PyQt6.QtCore import ( 13 from PyQt6.QtCore import (
13 QByteArray, 14 QByteArray,
14 QCryptographicHash, 15 QCryptographicHash,
22 from PyQt6.QtWebEngineCore import QWebEngineScript 23 from PyQt6.QtWebEngineCore import QWebEngineScript
23 24
24 from ..Tools.DelayedFileWatcher import DelayedFileWatcher 25 from ..Tools.DelayedFileWatcher import DelayedFileWatcher
25 from ..WebBrowserPage import WebBrowserPage 26 from ..WebBrowserPage import WebBrowserPage
26 from ..WebBrowserWindow import WebBrowserWindow 27 from ..WebBrowserWindow import WebBrowserWindow
27 from .GreaseMonkeyDownloader import GreaseMonkeyDownloader 28 from .GreaseMonkeyDownloader import GreaseMonkeyDownloader, GreaseMonkeyDownloadType
28 from .GreaseMonkeyJavaScript import bootstrap_js, values_js 29 from .GreaseMonkeyJavaScript import bootstrap_js, values_js
30
31
32 class GreaseMonkeyScriptStartPoint(enum.Enum):
33 """
34 Class defining the script start points.
35 """
36
37 DocumentStart = 0
38 DocumentEnd = 1
39 DocumentIdle = 2
29 40
30 41
31 class GreaseMonkeyScript(QObject): 42 class GreaseMonkeyScript(QObject):
32 """ 43 """
33 Class implementing the GreaseMonkey script. 44 Class implementing the GreaseMonkey script.
35 @signal scriptChanged() emitted to indicate a script change 46 @signal scriptChanged() emitted to indicate a script change
36 @signal updatingChanged(bool) emitted to indicate a change of the 47 @signal updatingChanged(bool) emitted to indicate a change of the
37 updating state 48 updating state
38 """ 49 """
39 50
40 # TODO: change this to an enum
41 DocumentStart = 0
42 DocumentEnd = 1
43 DocumentIdle = 2
44
45 scriptChanged = pyqtSignal() 51 scriptChanged = pyqtSignal()
46 updatingChanged = pyqtSignal(bool) 52 updatingChanged = pyqtSignal(bool)
47 53
48 def __init__(self, manager, path): 54 def __init__(self, manager, path):
49 """ 55 """
70 76
71 self.__icon = QIcon() 77 self.__icon = QIcon()
72 self.__iconUrl = QUrl() 78 self.__iconUrl = QUrl()
73 self.__downloadUrl = QUrl() 79 self.__downloadUrl = QUrl()
74 self.__updateUrl = QUrl() 80 self.__updateUrl = QUrl()
75 self.__startAt = GreaseMonkeyScript.DocumentEnd 81 self.__startAt = GreaseMonkeyScriptStartPoint.DocumentEnd
76 82
77 self.__script = "" 83 self.__script = ""
78 self.__fileName = path 84 self.__fileName = path
79 self.__enabled = True 85 self.__enabled = True
80 self.__valid = False 86 self.__valid = False
182 def startAt(self): 188 def startAt(self):
183 """ 189 """
184 Public method to get the start point of the script. 190 Public method to get the start point of the script.
185 191
186 @return start point of the script 192 @return start point of the script
187 @rtype DocumentStart or DocumentEnd 193 @rtype GreaseMonkeyScriptStartPoint
188 """ 194 """
189 return self.__startAt 195 return self.__startAt
190 196
191 def noFrames(self): 197 def noFrames(self):
192 """ 198 """
287 293
288 self.__icon = QIcon() 294 self.__icon = QIcon()
289 self.__iconUrl = QUrl() 295 self.__iconUrl = QUrl()
290 self.__downloadUrl = QUrl() 296 self.__downloadUrl = QUrl()
291 self.__updateUrl = QUrl() 297 self.__updateUrl = QUrl()
292 self.__startAt = GreaseMonkeyScript.DocumentEnd 298 self.__startAt = GreaseMonkeyScriptStartPoint.DocumentEnd
293 299
294 self.__script = "" 300 self.__script = ""
295 self.__enabled = True 301 self.__enabled = True
296 self.__valid = False 302 self.__valid = False
297 self.__noFrames = False 303 self.__noFrames = False
354 elif key == "@require": 360 elif key == "@require":
355 self.__require.append(value) 361 self.__require.append(value)
356 362
357 elif key == "@run-at": 363 elif key == "@run-at":
358 if value == "document-end": 364 if value == "document-end":
359 self.__startAt = GreaseMonkeyScript.DocumentEnd 365 self.__startAt = GreaseMonkeyScriptStartPoint.DocumentEnd
360 elif value == "document-start": 366 elif value == "document-start":
361 self.__startAt = GreaseMonkeyScript.DocumentStart 367 self.__startAt = GreaseMonkeyScriptStartPoint.DocumentStart
362 elif value == "document-idle": 368 elif value == "document-idle":
363 self.__startAt = GreaseMonkeyScript.DocumentIdle 369 self.__startAt = GreaseMonkeyScriptStartPoint.DocumentIdle
364 370
365 elif key == "@downloadURL" and self.__downloadUrl.isEmpty(): 371 elif key == "@downloadURL" and self.__downloadUrl.isEmpty():
366 self.__downloadUrl = QUrl(value) 372 self.__downloadUrl = QUrl(value)
367 373
368 elif key == "@updateURL" and self.__updateUrl.isEmpty(): 374 elif key == "@updateURL" and self.__updateUrl.isEmpty():
419 self.updatingChanged.emit(self.__updating) 425 self.updatingChanged.emit(self.__updating)
420 426
421 downloader = GreaseMonkeyDownloader( 427 downloader = GreaseMonkeyDownloader(
422 self.__downloadUrl, 428 self.__downloadUrl,
423 self.__manager, 429 self.__manager,
424 GreaseMonkeyDownloader.DownloadMainScript, 430 GreaseMonkeyDownloadType.MainScript,
425 ) 431 )
426 downloader.updateScript(self.__fileName) 432 downloader.updateScript(self.__fileName)
427 downloader.finished.connect(lambda: self.__downloaderFinished(downloader)) 433 downloader.finished.connect(lambda: self.__downloaderFinished(downloader))
428 downloader.error.connect(lambda: self.__downloaderError(downloader)) 434 downloader.error.connect(lambda: self.__downloaderError(downloader))
429 self.__downloaders.append(downloader) 435 self.__downloaders.append(downloader)
472 for urlStr in self.__require: 478 for urlStr in self.__require:
473 if not self.__manager.requireScripts([urlStr]): 479 if not self.__manager.requireScripts([urlStr]):
474 downloader = GreaseMonkeyDownloader( 480 downloader = GreaseMonkeyDownloader(
475 QUrl(urlStr), 481 QUrl(urlStr),
476 self.__manager, 482 self.__manager,
477 GreaseMonkeyDownloader.DownloadRequireScript, 483 GreaseMonkeyDownloadType.RequireScript,
478 ) 484 )
479 downloader.finished.connect( 485 downloader.finished.connect(
480 lambda: self.__requireDownloaded(downloader) 486 lambda: self.__requireDownloaded(downloader)
481 ) 487 )
482 downloader.error.connect( 488 downloader.error.connect(

eric ide

mercurial