def testBug899(self): html = ''' <html><body> <script type="text/javascript"> document.write("<p>"+py_obj.list1+"</p>") document.write("<p>"+py_obj.list2+"</p>") </script> </body></html> ''' class Obj(object): list1 = ['foo', 'bar', 'baz'] list2 = ['fi', 'fo', 'fum'] obj = Obj() wrapper_dict = {} for name in ('list1', 'list2'): getter = lambda arg=None, name=name: getattr(obj, name) wrapper_dict[name] = Property('QVariantList', getter) wrapper = type('PyObj', (QObject,), wrapper_dict) view = QWebView() view.page().mainFrame().addToJavaScriptWindowObject('py_obj', wrapper()) view.setHtml(html)
class TextMediaView(MediaView): def __init__(self, media, parent): super(TextMediaView, self).__init__(media, parent) self.widget = QWebView(parent) self.widget.setGeometry(media['geometry']) self.set_default_widget_prop() self.widget.setDisabled(True) self.widget.page().mainFrame().setScrollBarPolicy( Qt.Vertical, Qt.ScrollBarAlwaysOff) self.widget.page().mainFrame().setScrollBarPolicy( Qt.Horizontal, Qt.ScrollBarAlwaysOff) @Slot() def play(self): self.finished = 0 path = '%s/%s_%s_%s.html' % (self.save_dir, self.layout_id, self.region_id, self.id) self.widget.load('file://' + path) self.widget.show() self.widget.raise_() if float(self.duration) > 0: self.play_timer.setInterval(int(float(self.duration) * 1000)) self.play_timer.start() self.started_signal.emit() @Slot() def stop(self, delete_widget=False): #---- kong ---- if not self.widget: return False super(TextMediaView, self).stop(delete_widget) return True
class WebMediaView(MediaView): def __init__(self, media, parent): super(WebMediaView, self).__init__(media, parent) self._widget = QWebView(parent) self._widget.setGeometry(media['_geometry']) self.set_default_widget_prop() self._widget.setDisabled(True) self._widget.page().mainFrame().setScrollBarPolicy( Qt.Vertical, Qt.ScrollBarAlwaysOff) self._widget.page().mainFrame().setScrollBarPolicy( Qt.Horizontal, Qt.ScrollBarAlwaysOff) @Slot() def play(self): self._finished = 0 path = "%s/%s_%s_%s.html" % (self._save_dir, self._layout_id, self._region_id, self._id) self._widget.load("about:blank") if 'webpage' == str(self._type) and 'native' == str(self._render): url = self._options['uri'] self._widget.load(QUrl.fromPercentEncoding(url)) else: self._widget.load('file://' + path) self._widget.show() self._widget.raise_() self._play_timer.setInterval(int(float(self._duration) * 1000)) self._play_timer.start() self.started_signal.emit()
class TestJsCall(UsesQApplication): @classmethod def setUpClass(self): super(TestJsCall, self).setUpClass() def createInstance(self): global functionID self._view = QWebView() self._jsfuncs = JSFuncs() functionID = -1 self._view.page().mainFrame().addToJavaScriptWindowObject("jsfuncs", self._jsfuncs) self._view.loadFinished[bool].connect(self.onLoadFinished) self._view.load(PAGE_DATA % FUNCTIONS_LIST[self._functionID]) self._view.show() def testJsCall(self): self._functionID = 0 self.createInstance() self.app.exec_() def onLoadFinished(self, result): global functionID self.assertEqual(self._functionID, functionID) if self._functionID == (len(FUNCTIONS_LIST) - 1): QTimer.singleShot(300, self.app.quit) else: #new test self._functionID += 1 self.createInstance()
def testBug899(self): html = ''' <html><body> <script type="text/javascript"> document.write("<p>"+py_obj.list1+"</p>") document.write("<p>"+py_obj.list2+"</p>") </script> </body></html> ''' class Obj(object): list1 = ['foo', 'bar', 'baz'] list2 = ['fi', 'fo', 'fum'] obj = Obj() wrapper_dict = {} for name in ('list1', 'list2'): getter = lambda arg=None, name=name: getattr(obj, name) wrapper_dict[name] = Property('QVariantList', getter) wrapper = type('PyObj', (QObject, ), wrapper_dict) view = QWebView() view.page().mainFrame().addToJavaScriptWindowObject( 'py_obj', wrapper()) view.setHtml(html)
class Browser(QObject): # pylint: disable=too-many-public-methods __execute_script_called = Signal(str) def __init__(self): super().__init__() self.app = QApplication([]) self.webview = QWebView() self.webview.setAttribute(Qt.WA_DeleteOnClose) self.webview.destroyed.connect(self.app.quit) self.__execute_script_called.connect(self.__execute_script) def __execute_script(self, javascript_code: str): assert current_thread_is_main() self.webview.page().mainFrame().evaluateJavaScript(javascript_code) def execute_script(self, javascript_code: str): if current_thread_is_main(): self.__execute_script(javascript_code) else: self.__execute_script_called.emit(javascript_code) def run(self, url): assert current_thread_is_main() self.webview.showFullScreen() self.webview.load(url) self.app.exec_()
class TestJsCall(UsesQApplication): @classmethod def setUpClass(self): super(TestJsCall, self).setUpClass() def createInstance(self): global functionID self._view = QWebView() self._jsfuncs = JSFuncs() functionID = -1 self._view.page().mainFrame().addToJavaScriptWindowObject( "jsfuncs", self._jsfuncs) self._view.loadFinished[bool].connect(self.onLoadFinished) self._view.load(PAGE_DATA % FUNCTIONS_LIST[self._functionID]) self._view.show() def testJsCall(self): self._functionID = 0 self.createInstance() self.app.exec_() def onLoadFinished(self, result): global functionID self.assertEqual(self._functionID, functionID) if self._functionID == (len(FUNCTIONS_LIST) - 1): QTimer.singleShot(300, self.app.quit) else: #new test self._functionID += 1 self.createInstance()
class WebMediaView(MediaView): def __init__(self, media, parent): super(WebMediaView, self).__init__(media, parent) self._widget = QWebView(parent) self._widget.setGeometry(media['_geometry']) self.set_default_widget_prop() self._widget.setDisabled(True) self._widget.page().mainFrame().setScrollBarPolicy(Qt.Vertical, Qt.ScrollBarAlwaysOff) self._widget.page().mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff) @Slot() def play(self): self._finished = 0 path = "%s/%s_%s_%s.html" % ( self._save_dir, self._layout_id, self._region_id, self._id ) self._widget.load("about:blank") if 'webpage' == str(self._type) and 'native' == str(self._render): url = self._options['uri'] self._widget.load(QUrl.fromPercentEncoding(url)) else: self._widget.load('file://' + path) self._widget.show() self._widget.raise_() self._play_timer.setInterval(int(float(self._duration) * 1000)) self._play_timer.start() self.started_signal.emit()
def createWebView( nIndex ): global functionID global currentWebView functionID = nIndex currentWebView = QWebView() currentWebView._jsfuncs = JSFuncs() currentWebView.page().mainFrame().addToJavaScriptWindowObject("jsfuncs", currentWebView._jsfuncs) QObject.connect( currentWebView, QtCore.SIGNAL('loadFinished( bool )'), onLoadFinished ) currentWebView.load(PAGE_DATA % FUNCTIONS_LIST[ nIndex ]) currentWebView.show()
def new_tab(self): """Open new tab.""" tasklist = QTreeView() tasklist.hide() tasklist.setObjectName('taskList') tasklist.setMinimumWidth(100) tasklist.setMaximumWidth(250) new_tab = QWebView() new_tab.setObjectName('webView') inspector = QWebInspector(self) inspector.setObjectName('webInspector') inspector.hide() page_layout = QVBoxLayout() page_layout.setSpacing(0) page_layout.setContentsMargins(0, 0, 0, 0) page_layout.addWidget(new_tab) page_layout.addWidget(inspector) page_widget = QFrame() page_widget.setObjectName('pageWidget') page_widget.setLayout(page_layout) complete_tab_layout = QHBoxLayout() complete_tab_layout.setSpacing(0) complete_tab_layout.setContentsMargins(0, 0, 0, 0) complete_tab_layout.addWidget(tasklist) complete_tab_layout.addWidget(page_widget) complete_tab_widget = QFrame() complete_tab_widget.setLayout(complete_tab_layout) new_tab.load(QUrl(self.startpage)) self.tabs.setUpdatesEnabled(False) if self.new_tab_behavior == "insert": self.tabs.insertTab(self.tabs.currentIndex()+1, complete_tab_widget, unicode(new_tab.title())) elif self.new_tab_behavior == "append": self.tabs.appendTab(complete_tab_widget, unicode(new_tab.title())) self.tabs.setCurrentWidget(complete_tab_widget) self.tabs.setTabText(self.tabs.currentIndex(), unicode(self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).title())) self.tabs.setUpdatesEnabled(True) # tab.page().mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff) # tab.page().mainFrame().setScrollBarPolicy(Qt.Vertical, Qt.ScrollBarAlwaysOff) new_tab.titleChanged.connect(self.change_tab) new_tab.urlChanged.connect(self.change_tab) new_tab.loadStarted.connect(self.load_start) new_tab.loadFinished.connect(self.load_finish) new_tab.loadProgress.connect(self.pbar.setValue) new_tab.page().linkHovered.connect(self.linkHover) inspector.setPage(new_tab.page())
def testPlugin(self): view = QWebView() fac = PluginFactory() view.page().setPluginFactory(fac) QWebSettings.globalSettings().setAttribute(QWebSettings.PluginsEnabled, True) view.load(QUrl(os.path.join(os.path.abspath(os.path.dirname(__file__)), "qmlplugin", "index.html"))) view.resize(840, 600) view.show() QTimer.singleShot(500, self.app.quit) self.app.exec_()
def main(): app = QApplication([]) view = QWebView() fac = PluginFactory() view.page().setPluginFactory(fac) QWebSettings.globalSettings().setAttribute(QWebSettings.PluginsEnabled, True) view.load(QUrl(sys.argv[1])) view.resize(840, 600) view.show() return app.exec_()
class Robot(object): def __init__(self): self.view = QWebView() self.page = self.view.page() def start(self): self.view.show() QtCore.QObject.connect(self.view, QtCore.SIGNAL("loadFinished(bool)"), self.loaded) self.view.load(QUrl('https://login.yahoo.com/config/login_verify2?&.src=ym')) def loaded(self): url = self.view.url().toString() self.view.show() print "Loading %s finished" % url frame = self.page.mainFrame() els = frame.findAllElements("a[id*='copyright']") for i in range(els.count()): print 'Plain Text [%s]' % els.at(i).toPlainText().encode('utf8','ignore') print 'inner xml %s' % els.at(i).toInnerXml() child_els = els.at(i).findAll('*') for j in range(child_els.count()): print 'childs inner xml [%s] ' % child_els.at(j).toInnerXml()
def main(): app = QApplication([]) webview = QWebView() loop = QEventLoop() # 创建QEventLoop对象,该对象用于创建本地时间循环 webview.loadFinished.connect(loop.quit) # QWebView对象的loadFinished回调连接了QEventLoop的quit方法,从而可以在网页加载完成之后停止事件循环 webview.load(QUrl('http://example.webscraping.com/places/default/search')) loop.exec_() # QwebView的加载方法是异步的,执行过程会在网页加载时立即传入下一行, # 但我们希望等待网页加载完成,因此需要在事件循环启动时调用loop.exec_() webview.show() # 调用QWebViewGUI的show()方法来显示渲染窗口,以方便调试 frame = webview.page().mainFrame() # 创建一个指代框架的变量 # 使用CSS模式在框架中定位元素,然后设置搜索参数 frame.findFirstElement('#search_term').setAttribute('Value', '.') frame.findFirstElement('#page_size option:checked').setPlainText('1000') # 使用evaluate JavaScript()方法提交事件,模拟点击事件 frame.findFirstElement('#search').evaluateJavaScript('this.click()') app.exec_() # 进入应用的事件循环,可以对表单操作进行复查,如果没有使用该方法,脚本就会直接结束 # 等待结果,三种方法处理: # 1、等待一定时间,期望AJAX事件能够在此时刻之前完成 # 2、重写Qt的网络管理器,跟踪URL请求完成的事件 # 3、轮询网页,等待特定内容出现,下面采用第三种方法 elements = None while not elements: app.processEvents() # 用于给Qt事件循环执行任务的事件,比如响应点击事件和更新GUI elements = frame.findAllElements('#results a') countries = [e.toPlainText().strip() for e in elements] print countries
def get_site_text(url='https://test.api.unistream.com/help/index.html'): """Функция возвращает содержимое по указанному url.""" import sys from PySide.QtGui import QApplication from PySide.QtCore import QEventLoop from PySide.QtWebKit import QWebSettings, QWebPage, QWebView from PySide.QtNetwork import QNetworkProxyFactory # Чтобы не было проблем запуска компов с прокси: QNetworkProxyFactory.setUseSystemConfiguration(True) QWebSettings.globalSettings().setAttribute( QWebSettings.DeveloperExtrasEnabled, True) class WebPage(QWebPage): def userAgentForUrl(self, url): return 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Firefox/45.0' if QApplication.instance() is None: QApplication(sys.argv) view = QWebView() view.setPage(WebPage()) view.load(url) # Ждем пока прогрузится страница loop = QEventLoop() view.loadFinished.connect(loop.quit) loop.exec_() doc = view.page().mainFrame().documentElement() print(len(doc.toOuterXml()), len(doc.toPlainText())) return doc.toPlainText()
def webkit_download(url): app = QApplication([]) webview = QWebView() webview.loadFinished.connect(app.quit) webview.load(url) app.exec_() # delay here until download finished return webview.page().mainFrame().toHtml()
class HTMLApplication(object): def show(self): #It is IMPERATIVE that all forward slashes are scrubbed out, otherwise QTWebKit seems to be # easily confused kickOffHTML = join(dirname(__file__).replace('\\', '/'), "templates/index.html").replace('\\', '/') #This is basically a browser instance self.web = QWebView() #Unlikely to matter but prefer to be waiting for callback then try to catch # it in time. self.web.loadFinished.connect(self.onLoad) self.web.load(kickOffHTML) self.web.show() def onLoad(self): #This is the body of a web browser tab self.myPage = self.web.page() self.myPage.settings().setAttribute(QWebSettings.DeveloperExtrasEnabled, True) #This is the actual context/frame a webpage is running in. # Other frames could include iframes or such. self.myFrame = self.myPage.mainFrame() # ATTENTION here's the magic that sets a bridge between Python to HTML self.myFrame.addToJavaScriptWindowObject("eth", self.ethclient) #Tell the HTML side, we are open for business self.myFrame.evaluateJavaScript("ApplicationIsReady()")
class HTMLApplication(object): def show(self): #It is IMPERATIVE that all forward slashes are scrubbed out, otherwise QTWebKit seems to be # easily confused kickOffHTML = join( dirname(__file__).replace('\\', '/'), "templates/index.html").replace('\\', '/') #This is basically a browser instance self.web = QWebView() #Unlikely to matter but prefer to be waiting for callback then try to catch # it in time. self.web.loadFinished.connect(self.onLoad) self.web.load(kickOffHTML) self.web.show() def onLoad(self): #This is the body of a web browser tab self.myPage = self.web.page() self.myPage.settings().setAttribute( QWebSettings.DeveloperExtrasEnabled, True) #This is the actual context/frame a webpage is running in. # Other frames could include iframes or such. self.myFrame = self.myPage.mainFrame() # ATTENTION here's the magic that sets a bridge between Python to HTML self.myFrame.addToJavaScriptWindowObject("eth", self.ethclient) #Tell the HTML side, we are open for business self.myFrame.evaluateJavaScript("ApplicationIsReady()")
def testQVariantListProperty(self): class Obj(object): list = ['foo', 'bar', 'baz'] obj = Obj() wrapper_dict = {} for name in ['list']: getter = lambda arg=None, name=name: getattr(obj, name) wrapper_dict[name] = Property('QVariantList', getter) wrapper = type('PyObj', (QObject,), wrapper_dict) view = QWebView() frame = view.page().mainFrame() frame.addToJavaScriptWindowObject('py_obj', wrapper()) html = ''' <html><body> <script type="text/javascript"> document.write(py_obj.list) </script> </body></html> ''' view.setHtml(html) view.show() self.app.exec_()
class Event(QWidget): def __init__(self, parent=None): super(Event, self).__init__(parent) self.setWindowTitle('loadFinished Event') url = QUrl('https://www.torproject.org/') self.webView = QWebView() self.webView.setUrl(url) page = self.webView.page() frame = page.mainFrame() frame.loadFinished.connect(self.load_finished) self.mainLayout = QGridLayout() self.mainLayout.addWidget(self.webView) self.setLayout(self.mainLayout) def load_finished(self): print 'load has finished'
def testPlugin(self): view = QWebView() fac = PluginFactory() view.page().setPluginFactory(fac) QWebSettings.globalSettings().setAttribute(QWebSettings.PluginsEnabled, True) view.load( QUrl( os.path.join(os.path.abspath(os.path.dirname(__file__)), 'qmlplugin', 'index.html'))) view.resize(840, 600) view.show() QTimer.singleShot(500, self.app.quit) self.app.exec_()
class BrowserRender(QWebView): def __init__(self, display=True): self.app = QApplication([]) self.view = QWebView() self.loop = QEventLoop() def open(self, url, timeout=60): self.view.loadFinished.connect(self.loop.quit) self.view.load(QUrl(url)) self.loop.exec_() self.view.show() def html(self): return self.view.page().mainFrame().toHtml() def find(self, pattern): return self.view.page().mainFrame().findAllElements(pattern) def attr(self, pattern, name, value): for e in self.find(pattern): e.setAttribute(name, value) def text(self, pattern, value): for e in self.find(pattern): e.setPlainText(value) def click(self, pattern): for e in self.find(pattern): e.evaluateJavaScript('this.click()') def wait_load(self, pattern, timeout=60): deadline = time.time() + timeout while time.time() < deadline: self.app.processEvents() matches = self.find(pattern) if matches: return matches print 'Wait load time out' def exec_(self): self.app.exec_()
def main(): app = QApplication(sys.argv) view = QWebView() frame = view.page().mainFrame() printer = ConsolePrinter() view.setHtml(html) frame.addToJavaScriptWindowObject('printer', printer) frame = frame #frame.evaluateJavaScript("alert('Hello');") #frame.evaluateJavaScript("printer.text('Goooooooooo!');") view.show() app.processEvents() app.exec_()
def main(): app = QApplication([]) webview = QWebView() loop = QEventLoop() webview.loadFinished.connect(loop.quit) webview.load(QUrl('http://example.webscraping.com/search')) loop.exec_() webview.show() frame = webview.page().mainFrame() frame.findFirstElement('#search_term').setAttribute('value', '.') frame.findFirstElement('#page_size option:checked').setPlainText('1000') frame.findFirstElement('#search').evaluateJavaScript('this.click()') elements = None while not elements: app.processEvents()
class TestLoadFinished(TimedQApplication): '''Test case for signal QWebView.loadFinished(bool)''' def setUp(self): #Acquire resources TimedQApplication.setUp(self, timeout=1000) self.view = QWebView() QObject.connect(self.view, SIGNAL('loadFinished(bool)'), self.load_finished) self.called = False def tearDown(self): #Release resources del self.view self.called = False TimedQApplication.tearDown(self) def testLoadFinishedFromFile(self): url = QUrl.fromLocalFile(adjust_filename('fox.html', __file__)) self.view.setUrl(url) self.app.exec_() self.assert_(self.called) def testSetPageAndGetPage(self): twp = testWebPage() self.view.setPage(twp) del twp p = self.view.page() self.assertEqual(p.sayMyName(), 'testWebPage') # Setting the same webpage should not incref the python obj refCount = sys.getrefcount(p) self.view.setPage(p) self.assertEquals(sys.getrefcount(p), refCount) # Changing the webpage obj should decref the old one twp2 = testWebPage() self.view.setPage(twp2) self.assertEquals(sys.getrefcount(p), refCount - 1) def load_finished(self, ok): #Callback to check if load was successful self.app.quit() if ok: self.called = True
class HTMLApplication(object): def __init__(self, index_file, bridge = None, parent = None): self.index_file = index_file self.bridge = bridge self.mainWindow = QMainWindow(parent) self.mainWindow.setWindowTitle("My hub") self.mainWindow.setFixedSize(290,550); #This is basically a browser instance self.web = QWebView() self.mainWindow.setCentralWidget(self.web); def show(self): #It is IMPERATIVE that all forward slashes are scrubbed out, otherwise QTWebKit seems to be # easily confused #Asyncronously loads the website, in the interim you could throw up a splash screen # but this is relatively quick so it might not be worth it. self.web.loadFinished.connect(self.onLoad) self.web.setPage(WebPage()) self.web.load(self.index_file) self.mainWindow.show() #self.web.show() def onLoad(self): self.myPage = self.web.page() self.myPage.settings().setAttribute(QWebSettings.DeveloperExtrasEnabled, True) self.myPage.setNetworkAccessManager(WKNetworkAccessManager()) self.myFrame = self.myPage.mainFrame() for name, obj in self.bridge.items(): self.myFrame.addToJavaScriptWindowObject(name, obj) self.myFrame.evaluateJavaScript("ApplicationIsReady();") def onReload(self): print "reloaded"
def main(): app = QApplication([]) webview = QWebView() loop = QEventLoop() webview.loadFinished.connect(loop.quit) webview.load(QUrl('http://example.webscraping.com/places/default/search')) loop.exec_() webview.show() frame = webview.page().mainFrame() frame.findFirstElement('#search_term').setAttribute('value', '.') frame.findFirstElement('#page_size option').setPlainText('1000') #设置纯文本 frame.findFirstElement('#search').evaluateJavaScript('this.click()') app.exec_() elements = None while not elements: app.processEvents() elements = frame.findAllElements('#results a') countries = [e.toPlainText().strip() for e in elements] print countries
def main(): app = QApplication([]) webview = QWebView() loop = QEventLoop() webview.loadFinished.connect(loop.quit) webview.load(QUrl('http://example.webscraping.com/search')) loop.exec_() webview.show() frame = webview.page().mainFrame() frame.findFirstElement('#search_term').setAttribute('value', '.') frame.findFirstElement('#page_size option:checked').setPlainText('1000') frame.findFirstElement('#search').evaluateJavaScript('this.click()') elements = None while not elements: app.processEvents() elements = frame.findAllElements('#results a') countries = [e.toPlainText().strip() for e in elements] print countries
class MapApp(QWidget): def __init__(self, parent=None): # call QWidget's __init__ super(MapApp, self).__init__(parent) self.setWindowTitle("Map Application") f = open("map.html", "r") html = f.read() f.close() # declare our QWebView and set the URL to the source of the map.html file. # we must also set a URL (which QtWebKit will use if we ask for the URL of the page) self.webView = QWebView() self.webView.setHtml(html, baseUrl=QUrl("http://local")) self.webView.show() self.location = QLineEdit() self.go = QPushButton("Find this location") self.go.clicked.connect(self.findLocation) # set up our layout and add the instance of the QWebView self.mainLayout = QGridLayout() self.mainLayout.addWidget(self.location) self.mainLayout.addWidget(self.go) self.mainLayout.addWidget(self.webView) self.setLayout(self.mainLayout) def findLocation(self): page = self.webView.page() frame = page.mainFrame() # the javascript code to be executed functionCall = 'codeAddress("' + self.location.text() + '")' # execute the javascript code in the webkit instance frame.evaluateJavaScript(functionCall)
class Application(object): def show(self): self.battery = Battery() self.wifi = Wifi() self.url = QUrl("qt.html") self.web = QWebView() self.web.loadFinished.connect(self.onLoad) self.web.load(self.url) self.web.show() def onLoad(self): self.page = self.web.page() self.page.settings().setAttribute(QWebSettings.DeveloperExtrasEnabled, True) self.frame = self.page.mainFrame() self.frame.addToJavaScriptWindowObject("battery", self.battery) self.frame.addToJavaScriptWindowObject("wifi", self.wifi) self.frame.evaluateJavaScript("isReady()") self.wifi.loop.run()
def main(): ''' 首先设置搜索参数和模拟动作事件,获取在此参数和动作下搜索后得到的网页 然后在这网页下,获取数据 ''' app = QApplication([]) webview = QWebView() loop = QEventLoop() webview.loadFinished.connect(loop.quit) webview.load(QUrl('http://example.webscraping.com/places/default/search')) loop.exec_() webview.show() ## 显示渲染窗口,,可以直接在这个窗口里面输入参数,执行动作,方便调试 frame = webview.page().mainFrame() ## 设置搜索参数 # frame.findAllElements('#search_term') ##寻找所有的search_term框,返回的是列表 # frame.findAllElements('#page_size option:checked') # ## 表单使用evaluateJavaScript()方法进行提交,模拟点击事件 # frame.findAllElements('#search') frame.findFirstElement('#search_term').setAttribute('value', '.') ##第一个search_term框 frame.findFirstElement('#page_size option:checked').setPlainText( '1000') ##第一个page_size框 ## 表单使用evaluateJavaScript()方法进行提交,模拟点击事件 frame.findFirstElement('#search').evaluateJavaScript( 'this.click()') ##第一个点击框 ## 轮询网页,等待特定内容出现 ## 下面不断循环,直到国家链接出现在results这个div元素中,每次循环都会调用app.processEvents() ##用于给QT事件执行任务的时间,比如响应事件和更新GUI elements = None while not elements: app.processEvents() elements = frame.findAllElements('#results a') ##查找下载网页内的所有a标签 countries = [e.toPlainText().strip() for e in elements] ##取出所有a标签内的文本内容 print countries
class MainWindow(QMainWindow): def __init__(self, x_pox, y_pos, width, height): super(MainWindow, self).__init__() self.setGeometry(x_pox, y_pos, width, height) self.setMaximumSize(width, height) self.setMinimumSize(width, height) self.setWindowFlags(self.windowFlags() | Qt.WindowStaysOnTopHint) self.setWindowFlags(self.windowFlags() | Qt.Tool) self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint) self.setWindowFlags(self.windowFlags() | Qt.CustomizeWindowHint) self.setWindowFlags(self.windowFlags() & ~Qt.WindowMaximizeButtonHint) self.setWindowFlags(self.windowFlags() & ~Qt.WindowMinimizeButtonHint) self.setWindowFlags(self.windowFlags() & ~Qt.WindowSystemMenuHint) self.view = QWebView(self) self.view.page().setViewportSize(QSize(width, height)) self.view.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks) self.view.page().linkClicked.connect(self.link_click_handler) self.th = HNhtmlGenerator() self.th.started.connect(self.th_started) self.th.finished.connect(self.th_finished) super(MainWindow, self).setCentralWidget(self.view) def link_click_handler(self, url): if url.path() == u'blank': if url.hasFragment(): if url.fragment() == u'quit': QApplication.instance().quit() else: QDesktopServices.openUrl(url) def th_started(self): pass def th_finished(self): self.view.setHtml( self.th.getHtml() ) def showEvent(self, event): self.th.start()
class Browser(object): def __init__(self, callback, logger, options=None): self.logger = logger options = options or dict() self._request_ops = {'head': QNetworkAccessManager.HeadOperation, 'get': QNetworkAccessManager.GetOperation, 'put': QNetworkAccessManager.PutOperation, 'post': QNetworkAccessManager.PostOperation, 'delete': QNetworkAccessManager.DeleteOperation} self._timeout = int(options.pop('timeout', 30)) * 1000 max_request_retries = options.pop('max_request_retries', 3) self._network_manager = SmartNetworkAccessManager(logger, max_request_retries) self._web_page = CraftyWebPage() self._web_page.setNetworkAccessManager(self._network_manager) self._web_view = QWebView() self._web_view.setPage(self._web_page) # connect the loadFinished signal to a method defined by us. # loadFinished is the signal which is triggered when a page is loaded self._web_view.loadFinished.connect(self._load_finished) settings = self._web_view.settings() settings.setAttribute(QWebSettings.AutoLoadImages, options.pop('images', False)) settings.setAttribute(QWebSettings.JavascriptEnabled, options.pop('javascript', False)) settings.setAttribute(QWebSettings.JavascriptCanOpenWindows, options.pop('popups', False)) settings.setAttribute(QWebSettings.PrivateBrowsingEnabled, options.pop('private_browsing', False)) settings.setAttribute(QWebSettings.JavaEnabled, False) settings.setAttribute(QWebSettings.PluginsEnabled, False) settings.setAttribute(QWebSettings.DnsPrefetchEnabled, True) # store the callback function which will be called when a request is # finished self._result_callback = callback self._is_task_finished = False self._destroyed_status = dict() def _prepare_request(self, url, headers): # create an empty request request = QNetworkRequest() # assign a url to it request.setUrl(QUrl(url)) # add some custom headers to the request for (header_name, header_value) in headers.items(): request.setRawHeader(header_name, QByteArray(header_value)) return request def _urlencode_request_data(self, raw_data): # the data which we want to send to the server must be urlencoded request_data = QUrl() for (name, value) in raw_data.items(): request_data.addQueryItem(name, unicode(value)) return request_data.encodedQuery() def _load_finished(self, ok): """ Called when the page is fully loaded. It will get the html file of the loaded page and call the callback function with that result. """ if self._is_task_finished: # in case loadFinished fires more than once and we already # reported back with a result, don't do that again self.logger.info('loadFinished emitted, but task was already ' 'finished.') return pending_requests = self._network_manager.active_requests if ok == 'timed_out': self.logger.info('loadFinished emitted, request timed out.') self._network_manager.errors.append('Request timed out.') # to avoid treating the request by the driver as successful ok = False elif len(pending_requests) > 0: self.logger.info("loadFinished emitted, waiting for requests:" " {0}".format(pending_requests)) loaded = partial(lambda x: self._load_finished(x), ok) QTimer.singleShot(1000, loaded) return self.logger.info('loadFinshed emitted, returning result.') frame = self._web_view.page().mainFrame() url = smart_str(frame.url().toString()) html = frame.toHtml() result = {'html': html, 'url': url, 'successful': ok} if self._network_manager.errors: result['errors'] = self._network_manager.errors self._finish_task(result) def _start_task(self): self._is_task_finished = False # abusing the ok param of loadFinished timed_out = lambda: self._load_finished('timed_out') self._timeout_timer = QTimer() self._timeout_timer.timeout.connect(timed_out) self._timeout_timer.start(self._timeout) def _finish_task(self, result): self._is_task_finished = True self._timeout_timer.stop() # calling the callback function which we passed upon instantiation to # report the results there self._result_callback(result) def make(self, method, url, headers, raw_data=None): request = self._prepare_request(url, headers) operation = self._request_ops[method.lower()] request_data = self._urlencode_request_data(raw_data or dict()) self._start_task() self._web_view.load(request, operation, request_data) def _find_element(self, selector): main_frame = self._web_page.mainFrame() element = main_frame.findFirstElement(selector) if element.isNull(): raise ElementNotFound(selector) return element def fill_input(self, selector, value): js_fill_input = """ this.setAttribute("value", "{0}"); this.value = "{0}"; """.format(value) element = self._find_element(selector) element.evaluateJavaScript(js_fill_input) def click(self, selector): element = self._find_element(selector) offset = element.geometry() js_click = """ function mouse_click(element) {{ var event = document.createEvent('MouseEvents'); var offsetX = {0} + 2; //add 2 pixels otherwise it would var offsetY = {1} - 2; //seem like we clicked on the margin event.initMouseEvent( 'click', //event type true, //canBubble true, //cancelable document.defaultView, //view 1, //detail (window.screenX + offsetX), //screenX - The coords within (window.screenY + offsetY), //screenY - the entire page offsetX, //clientX - The coords within offsetY, //clientY - the viewport false, //ctrlKey false, //altKey false, //shiftKey false, //metaKey 0, //0=left, 1=middle, 2=right element //relatedTarget ); element.dispatchEvent(event); //Fire the event }} mouse_click(this);""".format(offset.left(), offset.top()) self._start_task() element.evaluateJavaScript(js_click) def _destroyed(self, component): self._destroyed_status[component] = True if all(self._destroyed_status.values()): self._shutdown_callback() def shutdown(self, callback): self._shutdown_callback = callback self._web_view.stop() self._web_view.close() # will immediately stop any running javascript code self._web_view.settings().setAttribute(QWebSettings.JavascriptEnabled, False) # if any requests were started by javascript after loadFinished was # emitted, and before we stopped javascript execution, cancel them self._network_manager.abort_requests() self._destroyed_status['web_page'] = False self._web_page.destroyed.connect(lambda: self._destroyed('web_page')) self._web_page.deleteLater() self._destroyed_status['web_view'] = False self._web_view.destroyed.connect(lambda: self._destroyed('web_view')) self._web_view.deleteLater() self._destroyed_status['network_manager'] = False destroyer = lambda: self._destroyed('network_manager') self._network_manager.destroyed.connect(destroyer) self._network_manager.deleteLater()
#将要加载的url传给QWebView,PyQt将该url的字符串封装在QUrl对象中 webview.load(QUrl(url)) # 等待网页加载完成,在事件循环启动时调用loop.exec_ loop.exec_() # 网页加载完成后退出事件循环 # html = webview.page().mainFrame().toHtml() # # 对加载的网页产生的HTMl进行数据抽取 # parser = etree.XMLParser(recover=True) # tree = etree.fromstring(html,parser) # print(tree.cssselect('#result')[0].text) webview.show() frame = webview.page().mainFrame() #找到id为search_term的标签,设置搜索值为.号(表示搜索全部结果) frame.findFirstElement('#search_term').setAttribute('value','.') # option:checked 选择每个被选中的 <option> 元素。此处表示找到id为page_size的标签下的option并选择值为1000 frame.findAllElements('#page_size option')[1].setPlainText('1000') #找到id为search的第一个标签,设置模拟点击事件 frame.findFirstElement('#search').evaluateJavaScript('this.click()') # app.exec_() elements=None while not elements: app.processEvents() elements = frame.findAllElements('#results a') countries = [e.toPlainText().strip() for e in elements] print(countries)
class BookView(QWidget): def __init__(self): QWidget.__init__(self) self.book = None self.toc = None self.current = None layout = QVBoxLayout(self) toolbar = QToolBar() toolbar.addAction('Open', self.openClicked) toolbar.addAction('Cover', self.coverClicked) toolbar.addAction('ToC', self.tocClicked) toolbar.addAction('Back', self.goBackClicked) toolbar.addAction('Forward', self.goForwardClicked) toolbar.addAction('Quit', self.quitClicked) layout.addWidget(toolbar) self.view = QWebView(self) layout.addWidget(self.view) self.setLayout(layout) def loadPage(self, contents): pageTempDir = tempfile.mkdtemp() self.view.setHtml(contents) page = self.view.page() epubFile = zipfile.ZipFile(self.book.filepath, 'r') epubFiles = epubFile.namelist() for element in page.mainFrame().findAllElements('img'): image = element.attribute('src') if not image.startswith('/') or not images.startswith('file://'): imageFile = None if image in epubFiles: imageFile = image elif os.path.join('OEBPS', image) in epubFiles: imageFile = os.path.join('OEBPS', image) elif os.path.join('OPS', image) in epubFiles: imageFile = os.path.join('OPS', image) else: continue epubFile.extract(imageFile, pageTempDir) element.setAttribute('src', 'file://%s/%s' % (pageTempDir, imageFile)) epubFile.close() page.setLinkDelegationPolicy(QWebPage.DelegateAllLinks) page.linkClicked[QUrl].connect(self.linkClicked) @staticmethod def findCoverImageFile(epubFilePath): epubFile = zipfile.ZipFile(epubFilePath, 'r') possible_cover_files = ['cover.png', 'cover.jpg', 'cover.jpeg'] coverFile = None for fileinfo in epubFile.filelist: if os.path.basename(fileinfo.filename).lower() in possible_cover_files: coverFile = fileinfo break return coverFile.filename if coverFile else None def loadBook(self, ebookfile): self.book = EPub.open(ebookfile) setattr(self.book, 'filepath', ebookfile) self.toc = [] self.showCover() # Load contents it = self.book.get_iterator(EIterator.SPINE) self.contents = {} for item in it: self.contents[item.curr_url()] = item.curr() title = self.book.get_metadata(EPub.TITLE)[0] self.setWindowTitle(title) def showCover(self): if hasattr(self.book, 'coverpage'): contents = self.book.coverpage else: # Load meta data title = self.book.get_metadata(EPub.TITLE) #creator = self.book.get_metadata(EPub.CREATOR) #subject = self.book.get_metadata(EPub.SUBJECT) #publisher = self.book.get_metadata(EPub.PUBLISHER) #date = self.book.get_metadata(EPub.DATE) #rights = self.book.get_metadata(EPub.RIGHTS) coverFile = BookView.findCoverImageFile(self.book.filepath) contents = '' if coverFile: alt = 'alt="%s"' % title if title else '' contents += u'<div align="center"><img src="%s" style="max-width: 400px;"/></div>' % coverFile #if title: #contents += u'<h1 align="center">%s</h1>' % u'<br/>'.join(title) #if creator: #def processAuthor(author): #aut = author[author.find(':')+1:] #pos = len(aut) #parenthesis = 0 #while pos != 0: #pos -= 1 #if aut[pos] == ')': #parenthesis += 1 #elif aut[pos] == '(': #parenthesis -= 1 #if parenthesis == 0: #aut = aut[:pos] #break #return aut.strip() #authors = [processAuthor(author) for author in creator] #contents += u'<p align="center">by<br/>%s</p>' % u',<br/>'.join(authors) #if publisher: #contents += u'<p align="center">Published by %s</p>' % u', '.join(publisher) #if rights: #contents += u'<p align="center">%s</p>' % u', '.join(rights) #if date: #contents += u'<p align="center">%s</p>' % u', '.join(date) #if subject: #contents += u'<p align="center">(%s)</p>' % u', '.join(subject) contents = u'<html><title>%s</title><body>%s</body></html>' % (u' - '.join(title), contents) setattr(self.book, 'coverpage', contents) self.loadPage(contents) self.current_position = 'coverpage' def showToC(self): if hasattr(self.book, 'tocpage'): contents = self.book.tocpage else: # Load Table of Contents title = self.book.get_metadata(EPub.TITLE) if title: contents = u'<h1 align="center">%s</h1>' % u'<br/>'.join(title) contents += u'<h2>Table of Contents</h2>' it = self.book.get_titerator(TIterator.NAVMAP) if not it: it = self.book.get_titerator(TIterator.GUIDE) for item in it: if item.isValid(): contents += u'<p><a href="%s">%s</a></p>' % (item.link(), item.label()) self.toc.append(item.link()) contents = u'<html><title>%s</title><body>%s</body></html>' % (u' - '.join(title), contents) setattr(self.book, 'tocpage', contents) self.loadPage(contents) self.current_position = 'tocpage' def linkClicked(self, url): fragment = None if url.hasFragment(): fragment = url.fragment() _url = QUrl(url.toString()[:url.toString().find('#%s'%fragment)]) if _url.toString() != 'about:blank' and _url.path() != self.current_position: url = _url fragment = None if fragment: page = self.view.page() page.mainFrame().scrollToAnchor(fragment) elif url.path() in self.contents.keys(): self.current_position = url.path() self.loadPage(self.contents[url.path()]) elif url.path().startswith('http://') or url.path().startswith('www.'): print 'pass the address to the system browser' def coverClicked(self): self.showCover() def openClicked(self): epubFile = QFileDialog.getOpenFileName(self, 'Open Image', '.', '*.epub')[0] if epubFile: self.loadBook(str(epubFile)) def tocClicked(self): self.showToC() def goBackClicked(self): if self.current_position == 'coverpage': return if self.current_position == 'tocpage': self.showCover() elif self.toc[0] == self.current_position: self.showToC() elif self.current_position != 'coverpage': self.current_position = self.toc[self.toc.index(self.current_position)-1] self.loadPage(self.contents[self.current_position]) def goForwardClicked(self): if self.current_position == 'coverpage': self.showToC() elif self.current_position == 'tocpage': self.current_position = self.toc[0] self.loadPage(self.contents[self.current_position]) elif self.current_position != self.toc[-1]: self.current_position = self.toc[self.toc.index(self.current_position)+1] self.loadPage(self.contents[self.current_position]) def quitClicked(self): QApplication.instance().quit()
import sys from albert_fixtures import url from PySide import QtGui from PySide import QtCore from PySide.QtWebKit import QWebView, QWebSettings app = QtGui.QApplication(sys.argv) win = QWebView() win.setUrl(QtCore.QUrl(url)) print "Getting frame..." frame = win.page().mainFrame().documentElement() print dir(win.page()) print "Getting button..." button = frame.findFirst("#video_mode > label:nth-child(5)") button.evaluateJavaScript("this.click()") print "Getting image..." image = frame.findFirst("#img1") print "Done." win.show() app.exec_() '''
URL = 'https://vk.com' if __name__ == '__main__': app = QApplication(sys.argv) view = QWebView() view.show() view.load(URL) loop = QEventLoop() view.loadFinished.connect(loop.quit) loop.exec_() doc = view.page().mainFrame().documentElement() email = doc.findFirst('#quick_email') password = doc.findFirst('#quick_pass') login_button = doc.findFirst('#quick_login_button') if email.isNull() or password.isNull() or login_button.isNull(): raise Exception('Ошибка при авторизации: не найдены поля емейла или пароля, или кнопка "Войти".') # Заполняем поля емейла/телефона и пароля email.setAttribute('value', LOGIN) password.setAttribute('value', PASSWORD) # Кликаем на кнопку "Войти" login_button.evaluateJavaScript('this.click()')
class Browser(object): def __init__(self, callback, logger, options=None): self.logger = logger self._request_ops = {'head': QNetworkAccessManager.HeadOperation, 'get': QNetworkAccessManager.GetOperation, 'put': QNetworkAccessManager.PutOperation, 'post': QNetworkAccessManager.PostOperation, 'delete': QNetworkAccessManager.DeleteOperation} self._network_manager = NetworkManager(logger) self._web_page = CraftyWebPage() self._web_page.setNetworkAccessManager(self._network_manager) self._web_view = QWebView() self._web_view.setPage(self._web_page) # connect the loadFinished signal to a method defined by us. # loadFinished is the signal which is triggered when a page is loaded self._web_view.loadFinished.connect(self._load_finished) options = options or dict() settings = self._web_view.settings() settings.setAttribute(QWebSettings.AutoLoadImages, options.pop('images', False)) settings.setAttribute(QWebSettings.JavascriptEnabled, options.pop('javascript', False)) settings.setAttribute(QWebSettings.JavascriptCanOpenWindows, options.pop('popups', False)) settings.setAttribute(QWebSettings.PrivateBrowsingEnabled, options.pop('private_browsing', False)) settings.setAttribute(QWebSettings.JavaEnabled, False) settings.setAttribute(QWebSettings.PluginsEnabled, False) settings.setAttribute(QWebSettings.DnsPrefetchEnabled, True) # store the callback function which will be called when a request is # finished self._result_callback = callback def _prepare_request(self, url, headers): # create an empty request request = QNetworkRequest() # assign a url to it request.setUrl(QUrl(url)) # add some custom headers to the request for (header_name, header_value) in headers.items(): request.setRawHeader(header_name, QByteArray(header_value)) return request def _urlencode_request_data(self, raw_data): # the data which we want to send to the server must be urlencoded request_data = QUrl() for (name, value) in raw_data.items(): request_data.addQueryItem(name, unicode(value)) return request_data.encodedQuery() def _load_finished(self, ok): """ Called when the page is fully loaded. It will get the html file of the loaded page and call the callback function with that result. """ frame = self._web_view.page().mainFrame() url = smart_str(frame.url().toString()) html = frame.toHtml() result = {'html': html, 'url': url, 'successful': True} if self._network_manager.errors: result['errors'] = self._network_manager.errors # calling the callback function which we passed upon instantiation to # report the results there self._result_callback(result) def make(self, method, url, headers, raw_data=None): request = self._prepare_request(url, headers) operation = self._request_ops[method.lower()] request_data = self._urlencode_request_data(raw_data or dict()) self._web_view.load(request, operation, request_data) def _find_element(self, selector): main_frame = self._web_page.mainFrame() element = main_frame.findFirstElement(selector) if element.isNull(): raise ElementNotFound(selector) return element def fill_input(self, selector, value): js_fill_input = """ this.setAttribute("value", "{0}"); this.value = "{0}"; """.format(value) element = self._find_element(selector) element.evaluateJavaScript(js_fill_input) def click(self, selector): element = self._find_element(selector) offset = element.geometry() js_click = """ function mouse_click(element) {{ var event = document.createEvent('MouseEvents'); var offsetX = {0} + 2; //add 2 pixels otherwise it would var offsetY = {1} - 2; //seem like we clicked on the margin event.initMouseEvent( 'click', //event type true, //canBubble true, //cancelable document.defaultView, //view 1, //detail (window.screenX + offsetX), //screenX - The coords within (window.screenY + offsetY), //screenY - the entire page offsetX, //clientX - The coords within offsetY, //clientY - the viewport false, //ctrlKey false, //altKey false, //shiftKey false, //metaKey 0, //0=left, 1=middle, 2=right element //relatedTarget ); element.dispatchEvent(event); //Fire the event }} mouse_click(this);""".format(offset.left(), offset.top()) element.evaluateJavaScript(js_click)
html = """ <html> <body> <h1>Hello!</h1><br> <h2><a href="#" onclick="printer.text('Message from QWebView')">QObject Test</a></h2> <h2><a href="#" onclick="alert('Javascript works!')">JS test</a></h2> </body> </html> """ class ConsolePrinter(QObject): def __init__(self, parent=None): super(ConsolePrinter, self).__init__(parent) @Slot(str) def text(self, message): print message if __name__ == '__main__': app = QApplication(sys.argv) view = QWebView() frame = view.page().mainFrame() printer = ConsolePrinter() view.setHtml(html) frame.addToJavaScriptWindowObject('printer', printer) frame.evaluateJavaScript("alert('Hello');") frame.evaluateJavaScript("printer.text('Goooooooooo!');") view.show() app.exec_()
def testReturnOwnership(self): app = QApplication(sys.argv) webview = QWebView() webview.page().networkAccessManager().sslErrors.connect(self.onError)
#!/usr/bin/env python import sys, os from PySide.QtCore import QObject, Slot, Property, QUrl, QSize, Qt from PySide.QtGui import QApplication, QDesktopServices from PySide.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkReply from PySide.QtWebKit import QWebView, QWebInspector, QWebPage, QWebSettings app = QApplication(sys.argv) wv = QWebView() wv.setContextMenuPolicy(Qt.NoContextMenu) page = wv.page() mainFrame = page.mainFrame() inspector = QWebInspector() inspector.setPage(page) class WebStoryNetworkAccessManager(QNetworkAccessManager): def __init__(self, parent=None): super(WebStoryNetworkAccessManager, self).__init__(parent) self.allowedUrls = set() self.allowedHosts = set() def addAllowedUrl(self, url): self.allowedUrls.add(url) def addAllowedHost(self, host):
class ViewerEditor(QStackedWidget): def __init__(self, parentWindow): QStackedWidget.__init__(self) self.__parentWindow= parentWindow self.__note= Note() self.__webNote= QWebView() webPage= self.__webNote.page() webPage.settings().setObjectCacheCapacities(0, 0, 0) webPage.action(QWebPage.Reload).setVisible(False) webPage.networkAccessManager().proxyAuthenticationRequired.connect(self.__onProxyAuthenticationRequired) self.updateProxy() self.__webNote.loadFinished.connect(self.updateHighlight) self.addWidget(self.__webNote) self.__editorNote= Editor(parentWindow, self.__onConfirm, self.__onCancel) self.addWidget(self.__editorNote) def noteUuid(self): return self.__note.uuid def view(self, note): self.__parentWindow.clearError() self.setCurrentIndex(0) self.__webNote.setHtml(note.html, rc.url()) self.__note= note def edit(self, note=None): self.__parentWindow.clearError() if note: self.__note= note self.setCurrentIndex(1) self.__editorNote.edit(self.__note) def isAllowedToChange(self): return self.__editorNote.isAllowedToChange() if self.currentIndex() == 1 else True def updateProxy(self): config= self.__parentWindow.config() if config.getProxyHost(): proxy= QNetworkProxy(QNetworkProxy.HttpProxy, config.getProxyHost(), config.getProxyPort()) else: proxy= QNetworkProxy() self.__webNote.page().networkAccessManager().setProxy(proxy) def updateHighlight(self): if self.currentIndex() != 0: return self.__webNote.findText(None, QWebPage.HighlightAllOccurrences) highlight= self.__parentWindow.textFilter() if highlight: for word in filter(lambda w: len(w) > 0, highlight.split(" ")): self.__webNote.findText(word, QWebPage.HighlightAllOccurrences) def __onConfirm(self, note): parent= self.__parentWindow if not note.title: parent.showError("A title is required") raise ValidationError noteProvider= parent.noteProvider() if note.uuid: noteProvider.update(renderHtml(note)) else: note.uuid= uuid() note.createdOn= note.lastModified noteProvider.add(renderHtml(note)) self.__note= note parent.reload(True, True) parent.showStatus("Note saved") def __onCancel(self): self.view(self.__note) def __onProxyAuthenticationRequired(self, proxy, authenticator): config= self.__parentWindow.config() if config.getProxyUser(): authenticator.setUser(config.getProxyUser()) authenticator.setPassword(config.getProxyPassword())
<h2><a href="#" onclick="printer.text('Message from QWebView')">QObject Test</a></h2> <h2><a href="#" onclick="alert('Javascript works!')">JS test</a></h2> </body> </html> """ class ConsolePrinter(QObject): def __init__(self, parent=None): super(ConsolePrinter, self).__init__(parent) @Slot(str) def text(self, msg): print msg if __name__ == "__main__": import sys app = QApplication(sys.argv) view = QWebView() printer = ConsolePrinter() frame = view.page().mainFrame() frame.addToJavaScriptWindowObject("printer", printer) frame.evaluateJavaScript('alert("hello world")') frame.evaluateJavaScript("printer.text(2222222222)") frame.setHtml(html) view.show() sys.exit(app.exec_())
class Form(QMainWindow): def __init__(self, home, parent=None, *, stayOnTop=False, debug=False): super().__init__(parent) if stayOnTop: flags = self.windowFlags() flags |= Qt.WindowStaysOnTopHint self.setWindowFlags(flags) self.home = home self.debug = debug self.filenamesForWord = {} self.titleForFilename = {} self.createWidgets() self.createLayout() self.createConnections() self.createShortcuts() self.changePage(self.home) self.updateUi() if self.debug: self.mtime = 0 self.timer = QTimer(self) self.timer.start(250) self.timer.timeout.connect(self.timeout) self.loadSettings() with open(Lib.get_path("doc/xix_style.css"), "r", encoding=UTF8) as file: self.css = file.read() self.browser.setFocus() self.setWindowTitle("Help — {}".format( QApplication.applicationName())) def timeout(self): mtime = self.get_mtime() if mtime and mtime != self.mtime: self.mtime = mtime self.browser.reload() def get_mtime(self): filename = self.browser.url().toLocalFile() try: return os.path.getmtime(filename) if filename else 0 except FileNotFoundError: time.sleep(0.1) return 0 def createWidgets(self): self.backButton = QToolButton() self.backButton.setIcon(QIcon(":/go-back.svg")) self.backButton.setText("&Back") self.backButton.setToolTip("""\ <p><b>Back</b> ({})</p> <p>Navigate to the previous page.</p>""".format( QKeySequence("Alt+Left").toString())) self.forwardButton = QToolButton() self.forwardButton.setIcon(QIcon(":/go-forward.svg")) self.forwardButton.setText("&Forward") self.forwardButton.setToolTip("""\ <p><b>Forward</b> ({})</p> <p>Navigate to the page you've just come back from.</p>""".format( QKeySequence("Alt+Right").toString())) self.contentsButton = QToolButton() self.contentsButton.setIcon(QIcon(":/go-home.svg")) self.contentsButton.setText("&Contents") self.contentsButton.setToolTip("""\ <p><b>Contents</b> ({})</p> <p>Navigate to the contents page.</p>""".format( QKeySequence("Alt+Home").toString())) self.searchLineEdit = Widgets.LegendLineEdit.LineEdit( "Search (F3 or Ctrl+F)") self.searchLineEdit.setToolTip("""\ <p><b>Search editor</p> <p>Type in a word to search for in the online help pages and press <b>Enter</b> or <b>F3</b> to search.</p>""") self.zoomInButton = QToolButton() self.zoomInButton.setIcon(QIcon(":/zoomin.svg")) self.zoomInButton.setText("&Zoom In") self.zoomInButton.setToolTip("""\ <p><b>Zoom In</b> ({})</p> <p>Make the text bigger.</p>""".format( QKeySequence("Alt++").toString())) self.zoomOutButton = QToolButton() self.zoomOutButton.setIcon(QIcon(":/zoomout.svg")) self.zoomOutButton.setText("Zoom &Out") self.zoomOutButton.setToolTip("""\ <p><b>Zoom Out</b> ({})</p> <p>Make the text smaller.</p>""".format( QKeySequence("Alt+-").toString())) width = self.fontMetrics().width(self.zoomOutButton.text() + " ") for button in (self.backButton, self.forwardButton, self.contentsButton, self.zoomInButton, self.zoomOutButton): button.setToolButtonStyle(Qt.ToolButtonTextUnderIcon) button.setMinimumWidth(width) button.setFocusPolicy(Qt.NoFocus) self.browser = QWebView() page = self.browser.page() page.setLinkDelegationPolicy(QWebPage.DelegateAllLinks) if self.debug: self.urlLabel = QLabel() def createLayout(self): hbox = QHBoxLayout() hbox.addWidget(self.backButton) hbox.addWidget(self.forwardButton) hbox.addWidget(self.contentsButton) hbox.addWidget(self.searchLineEdit, 1) hbox.addWidget(self.zoomInButton) hbox.addWidget(self.zoomOutButton) vbox = QVBoxLayout() vbox.addLayout(hbox) vbox.addWidget(self.browser, 1) if self.debug: vbox.addWidget(self.urlLabel) widget = QWidget() widget.setLayout(vbox) self.setCentralWidget(widget) def createConnections(self): self.browser.urlChanged.connect(self.updateUi) self.browser.linkClicked.connect(self.changePage) self.backButton.clicked.connect(self.browser.back) self.forwardButton.clicked.connect(self.browser.forward) self.contentsButton.clicked.connect( lambda: self.changePage(self.home)) self.searchLineEdit.returnPressed.connect(self.search) self.searchLineEdit.textEdited.connect(self.search) self.zoomInButton.clicked.connect(lambda: self.zoom(1.1)) self.zoomOutButton.clicked.connect(lambda: self.zoom(0.9)) def createShortcuts(self): QShortcut(QKeySequence(Qt.Key_Escape), self, self.close) QShortcut(QKeySequence("Alt+Left"), self, self.browser.back) QShortcut(QKeySequence("Alt+Right"), self, self.browser.forward) QShortcut(QKeySequence("Alt+Home"), self, self.contentsButton.click) QShortcut(QKeySequence("Ctrl++"), self, self.zoomInButton.click) QShortcut(QKeySequence("Ctrl+="), self, self.zoomInButton.click) QShortcut(QKeySequence("Ctrl+-"), self, self.zoomOutButton.click) QShortcut(QKeySequence("F3"), self, self.search) QShortcut(QKeySequence("Ctrl+F"), self, self.search) if self.debug: QShortcut(QKeySequence(Qt.Key_F5), self, self.browser.reload) def changePage(self, page): if isinstance(page, QUrl) and not page.scheme(): page = page.toString() if isinstance(page, str): url = QUrl.fromLocalFile(Lib.get_path(os.path.join("doc", page))) else: url = page if not url.isLocalFile(): webbrowser.open(url.toString()) return self.browser.setUrl(url) def search(self): self.searchLineEdit.setFocus() text = self.searchLineEdit.text() words = self.searchWordsSet(text) if not words: return if not self.filenamesForWord: self.populateUrlForWord() result = [HTML_TOP.format(self.css)] pages1, pages2 = self.acquirePageSets(words) if not pages1 and not pages2: result.append("""\ <h3><font color=darkgray>No help pages match <i>{}</i></font></h3>""" .format("</i> or <i>".join(text.split()))) else: n = len(pages1) + len(pages2) result.append("""\ <h3><font color=navy>{:,} help page{} match{} <i>{}</i>:</font></h3><ul>""" .format(n, "s" if n != 1 else "", "es" if n == 1 else "", "</i> or <i>".join(text.split()))) for page in itertools.chain(sorted(pages1), (None,), sorted(pages2)): line = ("</ul><br><ul>" if page is None else "<li><a href='{}'>{}</a></li>\n".format(page[1], page[0])) result.append(line) result.append("</ul>\n") result.append("</body></html>\n") self.browser.setHtml("".join(result)) def searchWordsSet(self, text): if len(text) < 2: return None words = set() for word in frozenset(re.split(r"\W+", text.strip().casefold())): words.add(word) words.add(Lib.stem(word)) return words def acquirePageSets(self, words): pages1 = set() pages2 = set() for word in words: filenames = self.filenamesForWord.get(word, set()) if filenames: for filename in filenames: title = self.titleForFilename[filename] if words & self.searchWordsSet( Lib.htmlToPlainText(title)): pages1.add((title, filename)) else: pages2.add((title, filename)) return pages1, pages2 def populateUrlForWord(self): self.filenamesForWord = {} path = Lib.get_path("doc") for filename in os.listdir(path): if filename.endswith(".html"): with open(os.path.join(path, filename), "r", encoding=UTF8) as file: text = file.read() match = re.search(r"<title>(.*?)</title>", text, re.IGNORECASE) title = filename if match is None else match.group(1) self.titleForFilename[filename] = title text = Lib.htmlToPlainText(text).casefold() for word in re.split(r"\W+", text): if word: self.filenamesForWord.setdefault( word, set()).add(filename) wordstem = Lib.stem(word) if wordstem and wordstem != word: self.filenamesForWord.setdefault( wordstem, set()).add(filename) def zoom(self, factor): factor *= self.browser.textSizeMultiplier() self.browser.setTextSizeMultiplier(factor) settings = QSettings() settings.setValue(HELP_ZOOM, factor) def updateUi(self): history = self.browser.history() self.backButton.setEnabled(history.canGoBack()) self.forwardButton.setEnabled(history.canGoForward()) self.contentsButton.setEnabled(self.browser.url() != self.home) if self.debug: self.urlLabel.setText(self.browser.url().toString()) self.mtime = self.get_mtime() def showEvent(self, event): self.loadSettings() def loadSettings(self): settings = QSettings() zoom = float(settings.value(HELP_ZOOM, 1.0)) if not Lib.isclose(zoom, 1.0): self.browser.setTextSizeMultiplier(zoom) self.restoreGeometry(settings.value(HELP_GEOMETRY)) def hideEvent(self, event): self.saveSettings() def closeEvent(self, event): self.saveSettings() def saveSettings(self): settings = QSettings() settings.setValue(HELP_GEOMETRY, self.saveGeometry())
class HTMLViewer(QtGui.QWidget): def __init__(self, parent=None): super(HTMLViewer, self).__init__(parent) self.view = QWebView(self) layout = QtGui.QVBoxLayout(self) layout.setSpacing(0) layout.setContentsMargins(0, 0, 0, 0) layout.addWidget(self.view) # Create ContextMenu context_menu = QtGui.QMenu(self.view) def open_context_menu(point): context_menu.exec_(self.view.mapToGlobal(point)) self.view.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) self.view.customContextMenuRequested.connect(open_context_menu) action = QtGui.QAction('&Back', self, shortcut=QtGui.QKeySequence.Back, statusTip="Click to go back", triggered=self.view.back) action.setShortcut('Backspace') context_menu.addAction(action) action = QtGui.QAction('&Forward', self, shortcut=QtGui.QKeySequence.Forward, statusTip="Click to go forward", triggered=self.view.forward) action.setShortcut('Shift + Backspace') context_menu.addAction(action) action = QtGui.QAction('&Reload', self, shortcut=QtGui.QKeySequence.Refresh, statusTip="Click to refresh", triggered=self.view.reload) context_menu.addAction(action) context_menu.addSeparator() action = QtGui.QAction('&Print', self, shortcut=QtGui.QKeySequence.Print, statusTip="Click to print", triggered=self.print_) context_menu.addAction(action) def contextMenuEvent(self, event): self.context_menu.exec_(event.globalPos()) def print_(self): printer = QtGui.QPrinter() printer_dialog = QtGui.QPrintDialog(printer, self.view) if printer_dialog.exec_() != QtGui.QDialog.Accepted: # do nothing return # printout the view with selected printer self.view.print_(printer) # --- slots @QtCore.Slot(unicode) def update(self, value): # save vertical/horizontal scrollbar value m = self.view.page().mainFrame() v = m.scrollBarValue(QtCore.Qt.Vertical) h = m.scrollBarValue(QtCore.Qt.Horizontal) # set new HTML (value should be unicode) self.view.setHtml(value) # update vertical/horizontal scrollbar value m = self.view.page().mainFrame() m.setScrollBarValue(QtCore.Qt.Vertical, v) m.setScrollBarValue(QtCore.Qt.Horizontal, h)
class ViewerEditor(QStackedWidget): def __init__(self, parentWindow): QStackedWidget.__init__(self) self.__parentWindow = parentWindow self.__note = Note() self.__webNote = QWebView() webPage = self.__webNote.page() webPage.settings().setObjectCacheCapacities(0, 0, 0) webPage.action(QWebPage.Reload).setVisible(False) webPage.networkAccessManager().proxyAuthenticationRequired.connect( self.__onProxyAuthenticationRequired) self.updateProxy() # noinspection PyUnresolvedReferences self.__webNote.loadFinished.connect(self.updateHighlight) self.addWidget(self.__webNote) self.__editorNote = Editor(parentWindow, self.__onConfirm, self.__onCancel) self.addWidget(self.__editorNote) def noteUuid(self): return self.__note.uuid def view(self, note): self.__parentWindow.clearError() self.setCurrentIndex(0) self.__webNote.setHtml(note.html, rc.url()) self.__note = note def edit(self, note=None): self.__parentWindow.clearError() if note: self.__note = note self.setCurrentIndex(1) self.__editorNote.edit(self.__note) def isAllowedToChange(self): return self.__editorNote.isAllowedToChange() if self.currentIndex( ) == 1 else True def updateProxy(self): config = self.__parentWindow.config() if config.getProxyHost(): proxy = QNetworkProxy(QNetworkProxy.HttpProxy, config.getProxyHost(), config.getProxyPort()) else: proxy = QNetworkProxy() self.__webNote.page().networkAccessManager().setProxy(proxy) def updateHighlight(self): if self.currentIndex() != 0: return self.__webNote.findText(None, QWebPage.HighlightAllOccurrences) highlight = self.__parentWindow.textFilter() if highlight: for word in filter(lambda w: len(w) > 0, highlight.split(" ")): self.__webNote.findText(word, QWebPage.HighlightAllOccurrences) def __onConfirm(self, note): parent = self.__parentWindow if not note.title: parent.showError("A title is required") raise ValidationError noteProvider = parent.noteProvider() if note.uuid: noteProvider.update(renderHtml(note)) else: note.uuid = newUuid() note.createdOn = note.lastModified noteProvider.add(renderHtml(note)) self.__note = note parent.reload(True, True) parent.showStatus("Note saved") def __onCancel(self): self.view(self.__note) # noinspection PyUnusedLocal def __onProxyAuthenticationRequired(self, proxy, authenticator): config = self.__parentWindow.config() if config.getProxyUser(): authenticator.setUser(config.getProxyUser()) authenticator.setPassword(config.getProxyPassword())
def __init__(self, parent=None): """Initialize the parent class of this instance.""" super(window, self).__init__(parent) app.aboutToQuit.connect(self.myExitHandler) self.style_sheet = self.styleSheet('style') # app.setStyle(QStyleFactory.create('Macintosh')) #app.setStyleSheet(self.style_sheet) self.layout().setSpacing(0) self.layout().setContentsMargins(0,0,0,0) app.setOrganizationName("Eivind Arvesen") app.setOrganizationDomain("https://github.com/eivind88/raskolnikov") app.setApplicationName("Raskolnikov") app.setApplicationVersion("0.0.1") settings = QSettings() self.data_location = QDesktopServices.DataLocation self.temp_location = QDesktopServices.TempLocation self.cache_location = QDesktopServices.CacheLocation self.startpage = "https://duckduckgo.com/" self.new_tab_behavior = "insert" global bookmarks global saved_tabs print "Currently saved_tabs:\n", saved_tabs global menubar menubar = QMenuBar() # Initialize a statusbar for the window self.statusbar = self.statusBar() self.statusbar.setFont(QFont("Helvetica Neue", 11, QFont.Normal)) self.statusbar.setStyleSheet(self.style_sheet) self.statusbar.setMinimumHeight(15) self.pbar = QProgressBar() self.pbar.setMaximumWidth(100) self.statusbar.addPermanentWidget(self.pbar) self.statusbar.hide() self.setMinimumSize(504, 235) # self.setWindowModified(True) # app.alert(self, 0) self.setWindowTitle("Raskolnikov") # toolbar = self.addToolBar('Toolbar') # toolbar.addAction(exitAction) # self.setUnifiedTitleAndToolBarOnMac(True) self.setWindowIcon(QIcon("")) # Create input widgets self.bbutton = QPushButton(u"<") self.fbutton = QPushButton(u">") self.hbutton = QPushButton(u"⌂") self.edit = QLineEdit("") self.edit.setFont(QFont("Helvetica Neue", 12, QFont.Normal)) self.edit.setPlaceholderText("Enter URL") # self.edit.setMinimumSize(400, 24) self.rbutton = QPushButton(u"↻") self.dbutton = QPushButton(u"☆") self.tbutton = QPushButton(u"⁐") # ↆ ⇧ √ ⌘ ⏎ ⏏ ⚠ ✓ ✕ ✖ ✗ ✘ ::: ❤ ☮ ☢ ☠ ✔ ☑ ♥ ✉ ☣ ☤ ✘ ☒ ♡ ツ ☼ ☁ ❅ ✎ self.nbutton = QPushButton(u"+") self.nbutton.setObjectName("NewTab") self.nbutton.setMinimumSize(35, 30) self.nbutton.setSizePolicy(QSizePolicy.Minimum,QSizePolicy.Minimum) self.edit.setTextMargins(2, 1, 2, 0) # create a horizontal layout for the input input_layout = QHBoxLayout() input_layout.setSpacing(4) input_layout.setContentsMargins(0, 0, 0, 0) # add the input widgets to the input layout input_layout.addWidget(self.bbutton) input_layout.addWidget(self.fbutton) input_layout.addWidget(self.hbutton) input_layout.addWidget(self.edit) input_layout.addWidget(self.rbutton) input_layout.addWidget(self.dbutton) input_layout.addWidget(self.tbutton) # create a widget to hold the input layout self.input_widget = QFrame() self.input_widget.setObjectName("InputWidget") self.input_widget.setStyleSheet(self.style_sheet) # set the layout of the widget self.input_widget.setLayout(input_layout) self.input_widget.setVisible(True) # CREATE BOOKMARK-LINE HERE self.bookmarks_layout = QHBoxLayout() self.bookmarks_layout.setSpacing(0) self.bookmarks_layout.setContentsMargins(0, 0, 0, 0) for i in bookmarks: link = QToolButton() link.setDefaultAction(QAction(unicode(i), link)) link.setObjectName(unicode(i)) link.setFont(QFont("Helvetica Neue", 11, QFont.Normal)) link.clicked.connect(self.handleBookmarks) self.bookmarks_layout.addWidget(link) self.bookmarks_widget = QFrame() self.bookmarks_widget.setObjectName("BookmarkWidget") self.bookmarks_widget.setStyleSheet(self.style_sheet) self.bookmarks_widget.setLayout(self.bookmarks_layout) if not bookmarks: self.bookmarks_widget.hide() # Task list self.tasklist = QStandardItemModel() #parentItem = self.tasklist.invisibleRootItem() #self.tasklist.header().hide() self.tasklist.setHorizontalHeaderItem(0, QStandardItem('Tasks')) parentItem = QStandardItem("Parent") self.tasklist.appendRow(parentItem) for i in range(4): item = QStandardItem("Item %d" % i) parentItem.appendRow(item) #parentItem = item #self.list.activated[str].connect(self.handleBookmarks) #self.list.view().setSizePolicy(QSizePolicy.Minimum,QSizePolicy.Minimum) # create tabs self.tabs = QTabWidget() self.tabs.setDocumentMode(True) self.tabs.setTabsClosable(True) self.tabs.setMovable(True) self.tabs.tabBar().hide() self.tabs.setFont(QFont("Helvetica Neue", 11, QFont.Normal)) self.tabs.setCornerWidget(self.nbutton) self.tabs.cornerWidget().setObjectName("CornerWidget") self.tabs.cornerWidget().setMinimumSize(10, 24) self.tabs.cornerWidget().setSizePolicy(QSizePolicy.Minimum,QSizePolicy.Minimum) if saved_tabs: for tab in saved_tabs['tabs']: tasklist = QTreeView() tasklist.hide() tasklist.setObjectName('taskList') tasklist.setMinimumWidth(100) tasklist.setMaximumWidth(250) new_tab = QWebView() new_tab.setObjectName('webView') inspector = QWebInspector(self) inspector.setObjectName('webInspector') inspector.hide() page_layout = QVBoxLayout() page_layout.setSpacing(0) page_layout.setContentsMargins(0, 0, 0, 0) page_layout.addWidget(new_tab) page_layout.addWidget(inspector) page_widget = QFrame() page_widget.setObjectName('pageWidget') page_widget.setLayout(page_layout) complete_tab_layout = QHBoxLayout() complete_tab_layout.setSpacing(0) complete_tab_layout.setContentsMargins(0, 0, 0, 0) complete_tab_layout.addWidget(tasklist) complete_tab_layout.addWidget(page_widget) complete_tab_widget = QFrame() complete_tab_widget.setLayout(complete_tab_layout) #for page in tab['history']: # new_tab.load(QUrl(page['url'])) #print tab['current_history'] #for item in new_tab.history().items(): # print item #new_tab.history().goToItem(new_tab.history().itemAt(tab['current_history'])) new_tab.load(QUrl(tab['history'][tab['current_history']]['url'])) tab['current_history'] self.tabs.setUpdatesEnabled(False) if self.new_tab_behavior == "insert": self.tabs.insertTab(self.tabs.currentIndex()+1, complete_tab_widget, unicode(new_tab.title())) elif self.new_tab_behavior == "append": self.tabs.appendTab(complete_tab_widget, unicode(new_tab.title())) self.tabs.setUpdatesEnabled(True) new_tab.titleChanged.connect(self.change_tab) new_tab.urlChanged.connect(self.change_tab) new_tab.loadStarted.connect(self.load_start) new_tab.loadFinished.connect(self.load_finish) new_tab.loadProgress.connect(self.pbar.setValue) new_tab.page().linkHovered.connect(self.linkHover) inspector.setPage(new_tab.page()) for index, tab in enumerate(saved_tabs['tabs']): self.tabs.setTabText(index, tab['history'][tab['current_history']]['title']) self.tabs.setCurrentIndex(saved_tabs['current_tab']) else: self.new_tab() tabs_layout = QVBoxLayout() tabs_layout.setSpacing(0) tabs_layout.setContentsMargins(0, 0, 0, 0) tabs_layout.addWidget(self.tabs) self.tabs_widget = QFrame() self.tabs_widget.setObjectName("TabLine") self.tabs_widget.setStyleSheet(self.style_sheet) self.tabs_widget.setLayout(tabs_layout) self.tabs_widget.setVisible(True) # Webkit settings gsettings = self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).settings().globalSettings() # Basic settings gsettings.setAttribute(QWebSettings.AutoLoadImages, True) gsettings.setAttribute(QWebSettings.JavascriptEnabled, True) gsettings.setAttribute(QWebSettings.JavascriptCanOpenWindows, False) gsettings.setAttribute(QWebSettings.JavascriptCanAccessClipboard, False) gsettings.setAttribute(QWebSettings.PluginsEnabled, False) # Flash isn't stable at present gsettings.setAttribute(QWebSettings.JavaEnabled, False) # Java applet's aren't supported by PySide gsettings.setAttribute(QWebSettings.DeveloperExtrasEnabled, True) gsettings.setAttribute(QWebSettings.AcceleratedCompositingEnabled, True) # Performace settings gsettings.setAttribute(QWebSettings.DnsPrefetchEnabled, True) gsettings.setAttribute(QWebSettings.AcceleratedCompositingEnabled, True) gsettings.setAttribute(QWebSettings.DnsPrefetchEnabled, True) # Other settings gsettings.setAttribute(QWebSettings.PrivateBrowsingEnabled, False) # Create a vertical layout and add widgets vlayout = QVBoxLayout() vlayout.setSpacing(0) vlayout.setContentsMargins(0, 0, 0, 0) # toolbar.addWidget(self.input_widget) vlayout.addWidget(self.input_widget) vlayout.addWidget(self.bookmarks_widget) vlayout.addWidget(self.tabs_widget) # create a widget to hold the vertical layout wrapper_widget = QWidget() wrapper_widget.setLayout(vlayout) self.setCentralWidget(wrapper_widget) self.bbutton.clicked.connect(self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).back) self.fbutton.clicked.connect(self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).forward) self.hbutton.clicked.connect(self.goHome) self.edit.returnPressed.connect(self.set_url) # Add button signal to "go" slot self.rbutton.clicked.connect(self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).reload) self.dbutton.clicked.connect(self.bookmark) self.tbutton.clicked.connect(self.toggleTaskBar) self.nbutton.clicked.connect(self.new_tab) self.tabs.tabCloseRequested.connect(self.tabs.removeTab) self.tabs.currentChanged.connect(self.change_tab) widgets = (input_layout.itemAt(i).widget() for i in range( input_layout.count())) for widget in widgets: if isinstance(widget, QPushButton): widget.setFixedSize(33, 21) widget.setFont(QFont("Helvetica Neue", 12, QFont.Normal)) widget.pressed.connect(self.press_button) widget.released.connect(self.release_button) # make a ctrl+q quit sequence = QKeySequence(Qt.CTRL + Qt.Key_Q) QShortcut(sequence, self, SLOT("close()")) # make an accelerator to toggle fullscreen sequence = QKeySequence(Qt.CTRL + Qt.SHIFT + Qt.Key_F) QShortcut(sequence, self, self.toggle_fullscreen) # make an accelerator to toggle input visibility sequence = QKeySequence(Qt.CTRL + Qt.ALT + Qt.Key_L) QShortcut(sequence, self, self.toggle_input) # make an accelerator to focus adress-bar sequence = QKeySequence(Qt.CTRL + Qt.Key_L) QShortcut(sequence, self, self.focus_adress) # make an accelerator to reload page sequence = QKeySequence(Qt.CTRL + Qt.Key_R) QShortcut(sequence, self, self.tabs.currentWidget().findChild(QFrame, unicode('pageWidget')).findChild(QWebView, unicode('webView')).reload) # make an accelerator to create new tab sequence = QKeySequence(Qt.CTRL + Qt.Key_T) QShortcut(sequence, self, self.new_tab) # make an accelerator to close tab sequence = QKeySequence(Qt.CTRL + Qt.Key_W) QShortcut(sequence, self, self.close_tab) # make an accelerator to navigate tabs sequence = QKeySequence(Qt.CTRL + Qt.SHIFT + Qt.Key_Left) QShortcut(sequence, self, self.previous_tab) sequence = QKeySequence(Qt.CTRL + Qt.SHIFT + Qt.Key_Right) QShortcut(sequence, self, self.next_tab) # make an accelerator to toggle inspector sequence = QKeySequence(Qt.CTRL + Qt.ALT + Qt.Key_U) QShortcut(sequence, self, self.handleShowInspector) # make an accelerator to toggle bookmark sequence = QKeySequence(Qt.CTRL + Qt.Key_D) QShortcut(sequence, self, self.bookmark) # make an accelerator to toggle task/project-list sequence = QKeySequence(Qt.CTRL + Qt.SHIFT + Qt.Key_L) QShortcut(sequence, self, self.toggleTaskBar) # finally set the attribute need to rotate # try: # self.setAttribute(Qt.WA_Maemo5AutoOrientation, True) # except: # print "not maemo" self.statusbar.show()
class Browser(QWidget): def __init__(self): super(Browser, self).__init__() self.layout = QStackedLayout(self) self.frontView = QWebView(self) self.backView = QWebView(self) self.frontView.page().networkAccessManager().sslErrors.connect(self.onSslErrors) self.backView.page().networkAccessManager().sslErrors.connect(self.onSslErrors) self.hostname = os.uname()[1] self.layout.setStackingMode(QStackedLayout.StackAll) self.layout.addWidget(self.frontView) self.layout.addWidget(self.backView) self.commands = { 'help' : self.cmdHelp, 'refresh' : self.cmdRefresh, 'add' : self.cmdAdd, 'del' : self.cmdDel, 'list' : self.cmdList, 'zoom' : self.cmdZoom, 'fs' : self.cmdFs, 'exit' : self.cmdExit, 'next' : self.cmdNext, 'restart' : self.cmdRestart, 'upgrade' : self.cmdUpgrade, } self.timer = QTimer() self.timer.setSingleShot(False) self.sites = list() self.siteId = -1 self.socket = False url = QUrl("http://dmr.tokiogroup.com/rrs.php") self.sites.append(Site(url, 10, 1)) url = QUrl("http://dmr.tokiogroup.com/lastheard.php") self.sites.append(Site(url, 10, 1)) self.server = QTcpServer() self.server.listen(QHostAddress.Any, 4242) self.connect(self.server, SIGNAL("newConnection()"), self, SLOT("onConnection()")); self.connect(self.timer, SIGNAL("timeout()"), self, SLOT("goNext()")); self.goNext() self.goNext() def goNext(self): self.cmdNext(list()) def onConnection(self): self.socket = self.server.nextPendingConnection() self.socket.write(self.hostname + ' % ') self.connect(self.socket, SIGNAL("readyRead()"), self, SLOT("onDataReceived()")); def print_(self, text): if (self.socket != False): self.socket.write(text + '\n') print(text) def onDataReceived(self): data = self.socket.readAll().data().strip() try: args = data.split(' ') map(lambda x : x.strip(), args) self.commands.get(args[0])(args) except Exception: self.print_('>> syntax error') self.printCommandsList() if self.socket.isOpen(): self.socket.write(self.hostname + ' % ') def printCommandsList(self): self.print_('avaible commands:') for command in self.commands: self.print_(' ' + command) def onSslErrors(self, reply, errors): url = unicode(reply.url().toString()) reply.ignoreSslErrors() # commands def cmdHelp(self, args): self.print_('>> help') self.printCommandsList() def cmdRefresh(self, args): self.print_('>> refresh ' + self.url.toString().encode()) self.frontView.reload() def cmdAdd(self, args): self.print_('>> add ' + args[1] + ' ' + args[2] + ' ' + args[3]) self.sites.append(Site(QUrl(args[1]), int(args[2], int(args[3])))) def cmdDel(self, args): self.print_('>> del ' + args[1]) self.sites.pop(int(args[1])) def cmdList(self, args): self.print_('>> list') self.print_('current list:') sitesCount = len(self.sites) i = 0 while i < sitesCount: self.print_('%1c[%1d] %2ds : %3s' % ((i==self.siteId) and '*' or ' ' ,i, self.sites[i].time, self.sites[i].url.toString().encode())) i = i + 1 def cmdZoom(self, args): self.print_('>> zoom ' + args[1]) self.frontView.setZoomFactor(float(args[1])) def cmdFs(self, args): self.print_('>> fs ' + args[1]) if (args[1] == '1'): self.showFullScreen() else: self.showNormal() def cmdExit(self, args): self.print_('>> exit') self.socket.close() def cmdNext(self, args): self.timer.stop() self.timer.start(self.sites[self.siteId].time * 1000) self.siteId = (self.siteId + 1) % len(self.sites) print('>> next ' + self.sites[self.siteId].url.toString().encode()) self.backView.show() self.frontView, self.backView = self.backView, self.frontView self.backView.load(self.sites[self.siteId].url) self.backView.setZoomFactor(self.sites[self.siteId].zoom) self.backView.hide() def cmdRestart(self, args): self.print_('>> restart') self.close() def cmdUpgrade(self, args): self.print_('>> upgrade') update = urllib.urlopen('https://raw.githubusercontent.com/tjardick/monitoring_browser/master/webkit.py').read() script = file('webkit.py', 'w') script.write(update) script.close() self.close()