eric6/WebBrowser/JavaScript/ExternalJsObject.py

Sat, 27 Feb 2021 12:08:23 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 27 Feb 2021 12:08:23 +0100
changeset 8138
169e65a6787c
parent 7923
91e843545d9a
child 8218
7c09585bd960
permissions
-rw-r--r--

Shell: added functionality to show a prompt when the main client process has exited (e.g. a script ended).

4717
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
2
7923
91e843545d9a Updated copyright for 2021.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7781
diff changeset
3 # Copyright (c) 2016 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4717
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
4 #
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
5
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
6 """
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
7 Module implementing the JavaScript external object being the endpoint of
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
8 a web channel.
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
9 """
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
10
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
11 #
4749
750577d35452 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4745
diff changeset
12 # This code was ported from QupZilla and modified.
4717
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
13 # Copyright (C) David Rosca <nowrep@gmail.com>
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
14 #
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
15
4868
985d275502c8 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4863
diff changeset
16 from PyQt5.QtCore import pyqtProperty, QObject
4717
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
17
4868
985d275502c8 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4863
diff changeset
18 from WebBrowser.WebBrowserWindow import WebBrowserWindow
4745
285bfd224a1b Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4744
diff changeset
19
4863
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
20 from .StartPageJsObject import StartPageJsObject
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
21 from .PasswordManagerJsObject import PasswordManagerJsObject
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
22
4717
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
23
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
24 class ExternalJsObject(QObject):
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
25 """
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
26 Class implementing the endpoint of our web channel.
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
27 """
6127
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
28 extraObjects = {}
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
29
4717
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
30 def __init__(self, page):
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
31 """
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
32 Constructor
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
33
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
34 @param page reference to the web page object
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
35 @type WebBrowserPage
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
36 """
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
37 super(ExternalJsObject, self).__init__(page)
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
38
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
39 self.__page = page
4863
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
40
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
41 self.__startPage = None
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
42 self.__passwordManager = None
4717
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
43
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
44 def page(self):
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
45 """
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
46 Public method returning a reference to the web page object.
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
47
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
48 @return reference to the web page object
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
49 @rtype WebBrowserPage
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
50 """
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
51 return self.__page
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
52
4863
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
53 @pyqtProperty(QObject, constant=True)
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
54 def passwordManager(self):
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
55 """
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
56 Public method to get a reference to the password manager JavaScript
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
57 object.
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
58
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
59 @return reference to the password manager JavaScript object
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
60 @rtype StartPageJsObject
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
61 """
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
62 if self.__passwordManager is None:
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
63 self.__passwordManager = PasswordManagerJsObject(self)
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
64
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
65 return self.__passwordManager
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
66
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
67 @pyqtProperty(QObject, constant=True)
4717
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
68 def speedDial(self):
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
69 """
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
70 Public method returning a reference to a speed dial object.
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
71
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
72 @return reference to a speed dial object
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
73 @rtype SpeedDial
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
74 """
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
75 if self.__page.url().toString() != "eric:speeddial":
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
76 return None
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
77
4868
985d275502c8 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4863
diff changeset
78 return WebBrowserWindow.speedDial()
4717
5841f229baf7 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
diff changeset
79
4863
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
80 @pyqtProperty(QObject, constant=True)
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
81 def startPage(self):
4749
750577d35452 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4745
diff changeset
82 """
4863
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
83 Public method to get a reference to the start page JavaScript object.
4861
d419f153925d Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4749
diff changeset
84
4863
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
85 @return reference to the start page JavaScript object
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
86 @rtype StartPageJsObject
4861
d419f153925d Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4749
diff changeset
87 """
4863
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
88 if self.__startPage is None:
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
89 self.__startPage = StartPageJsObject(self)
4861
d419f153925d Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4749
diff changeset
90
4863
9d86824898e1 Continued porting the web browser.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 4861
diff changeset
91 return self.__startPage
6127
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
92
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
93 @classmethod
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
94 def setupWebChannel(cls, channel, page):
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
95 """
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
96 Class method to setup the web channel.
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
97
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
98 @param channel reference to the channel
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
99 @type QWebChannel
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
100 @param page reference to the web page
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
101 @type QWebEnginePage
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
102 """
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
103 channel.registerObject("eric_object", ExternalJsObject(page))
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
104 for jsObject in cls.extraObjects:
6133
eba07240fac1 Fixed a typo.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6127
diff changeset
105 channel.registerObject("eric_{0}".format(jsObject),
6127
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
106 cls.extraObjects[jsObject])
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
107
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
108 @classmethod
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
109 def registerExtraObject(cls, name, jsObject):
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
110 """
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
111 Class method to register extra JavaScript objects.
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
112
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
113 @param name name for the object
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
114 @type str
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
115 @param jsObject reference to the JavaScript object to be registered
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
116 @type QObject
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
117 """
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
118 cls.extraObjects[id] = jsObject
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
119
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
120 @classmethod
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
121 def unregisterExtraObject(cls, name):
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
122 """
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
123 Class method to unregister extra JavaScript objects.
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
124
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
125 @param name name of the object
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
126 @type str
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
127 """
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
128 if name in cls.extraObjects:
128d9567a533 Some improvements of the Web Browser NG.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 6048
diff changeset
129 del cls.extraObjects[name]

eric ide

mercurial