eric6/WebBrowser/GreaseMonkey/GreaseMonkeyScript.py

branch
without_py2_and_pyqt4
changeset 7201
6b42677d7043
parent 6942
2602857055c5
child 7206
74666c6679af
equal deleted inserted replaced
7200:ebab8ba287e6 7201:6b42677d7043
19 from .GreaseMonkeyDownloader import GreaseMonkeyDownloader 19 from .GreaseMonkeyDownloader import GreaseMonkeyDownloader
20 20
21 from ..Tools.DelayedFileWatcher import DelayedFileWatcher 21 from ..Tools.DelayedFileWatcher import DelayedFileWatcher
22 from ..WebBrowserPage import WebBrowserPage 22 from ..WebBrowserPage import WebBrowserPage
23 from ..WebBrowserWindow import WebBrowserWindow 23 from ..WebBrowserWindow import WebBrowserWindow
24
25 from Globals import qVersionTuple
26 24
27 25
28 class GreaseMonkeyScript(QObject): 26 class GreaseMonkeyScript(QObject):
29 """ 27 """
30 Class implementing the GreaseMonkey script. 28 Class implementing the GreaseMonkey script.
360 358
361 nspace = bytes(QCryptographicHash.hash( 359 nspace = bytes(QCryptographicHash.hash(
362 QByteArray(self.fullName().encode("utf-8")), 360 QByteArray(self.fullName().encode("utf-8")),
363 QCryptographicHash.Md4).toHex()).decode("ascii") 361 QCryptographicHash.Md4).toHex()).decode("ascii")
364 valuesScript = values_js.format(nspace) 362 valuesScript = values_js.format(nspace)
365 if qVersionTuple() < (5, 8, 0): 363 self.__script = "(function(){{{0}\n{1}\n{2}\n}})();".format(
366 runCheck = """ 364 valuesScript, self.__manager.requireScripts(self.__require),
367 for (var value of {0}) {{ 365 fileData
368 var re = new RegExp(value); 366 )
369 if (re.test(window.location.href)) {{
370 return;
371 }}
372 }}
373 __eric_includes = false;
374 for (var value of {1}) {{
375 var re = new RegExp(value);
376 if (re.test(window.location.href)) {{
377 __eric_includes = true;
378 break;
379 }}
380 }}
381 if (!__eric_includes) {{
382 return;
383 }}
384 delete __eric_includes;""".format(
385 self.__toJavaScriptList(self.__exclude[:]),
386 self.__toJavaScriptList(self.__include[:])
387 )
388 self.__script = "(function(){{{0}\n{1}\n{2}\n{3}\n}})();".format(
389 runCheck, valuesScript,
390 self.__manager.requireScripts(self.__require), fileData
391 )
392 else:
393 self.__script = "(function(){{{0}\n{1}\n{2}\n}})();".format(
394 valuesScript, self.__manager.requireScripts(self.__require),
395 fileData
396 )
397 self.__valid = True 367 self.__valid = True
398 368
399 self.__downloadIcon() 369 self.__downloadIcon()
400 self.__downloadRequires() 370 self.__downloadRequires()
401 371
405 375
406 @return prepared script object 376 @return prepared script object
407 @rtype QWebEngineScript 377 @rtype QWebEngineScript
408 @exception ValueError raised to indicate an unsupported start point 378 @exception ValueError raised to indicate an unsupported start point
409 """ 379 """
410 if qVersionTuple() < (5, 8, 0):
411 if self.startAt() == GreaseMonkeyScript.DocumentStart:
412 injectionPoint = QWebEngineScript.DocumentCreation
413 elif self.startAt() == GreaseMonkeyScript.DocumentEnd:
414 injectionPoint = QWebEngineScript.DocumentReady
415 elif self.startAt() == GreaseMonkeyScript.DocumentIdle:
416 injectionPoint = QWebEngineScript.Deferred
417 else:
418 raise ValueError("Wrong script start point.")
419
420 script = QWebEngineScript() 380 script = QWebEngineScript()
421 script.setSourceCode("{0}\n{1}".format( 381 script.setSourceCode("{0}\n{1}".format(
422 bootstrap_js, self.__script 382 bootstrap_js, self.__script
423 )) 383 ))
424 script.setName(self.fullName()) 384 script.setName(self.fullName())
425 if qVersionTuple() < (5, 8, 0):
426 script.setInjectionPoint(injectionPoint)
427 script.setWorldId(WebBrowserPage.SafeJsWorld) 385 script.setWorldId(WebBrowserPage.SafeJsWorld)
428 script.setRunsOnSubFrames(not self.__noFrames) 386 script.setRunsOnSubFrames(not self.__noFrames)
429 return script 387 return script
430 388
389 # TODO: get rid of it
431 def __toJavaScriptList(self, patterns): 390 def __toJavaScriptList(self, patterns):
432 """ 391 """
433 Private method to convert a list of str to a string containing a valid 392 Private method to convert a list of str to a string containing a valid
434 JavaScript list definition. 393 JavaScript list definition.
435 394
436 @param patterns list of match patterns 395 @param patterns list of match patterns
437 @type list of str 396 @type list of str
438 @return JavaScript script containing the list 397 @return JavaScript script containing the list
439 @rtype str 398 @rtype str
440 """ 399 """
441 if qVersionTuple() >= (5, 8, 0): 400 return ""
442 script = ""
443 else:
444 patternList = []
445 for pattern in patterns:
446 if pattern.startswith("/") and pattern.endswith("/") and \
447 len(pattern) > 1:
448 pattern = pattern[1:-1]
449 else:
450 pattern = pattern.replace(".", "\\.").replace("*", ".*")
451 pattern = "'{0}'".format(pattern)
452 patternList.append(pattern)
453
454 script = "[{0}]".format(",".join(patternList))
455 return script
456 401
457 def updateScript(self): 402 def updateScript(self):
458 """ 403 """
459 Public method to updated the script. 404 Public method to updated the script.
460 """ 405 """

eric ide

mercurial