Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

146 rindas
4.6 KiB

  1. #-*- coding: UTF-8 -*-
  2. #/usr/bin/env python3
  3. from PyQt5.QtCore import QFile, QIODevice, Qt, QTextStream, QUrl
  4. from PyQt5.QtWidgets import (QAction, QApplication, QLineEdit, QMainWindow,
  5. QSizePolicy, QStyle, QTextEdit)
  6. from PyQt5.QtNetwork import QNetworkProxyFactory, QNetworkRequest
  7. from PyQt5.QtWebKitWidgets import QWebPage, QWebInspector, QWebView
  8. #import jquery_rc
  9. class MainWindow(QMainWindow):
  10. def __init__(self, url):
  11. super(MainWindow, self).__init__()
  12. self.progress = 0
  13. fd = QFile(":/views/static/js/brython.js")
  14. if fd.open(QIODevice.ReadOnly | QFile.Text):
  15. self.Bryton = QTextStream(fd).readAll()
  16. fd.close()
  17. else:
  18. self.Brython= ''
  19. QNetworkProxyFactory.setUseSystemConfiguration(True)
  20. self.view = QWebView(self)
  21. self.view.load(url)
  22. self.view.loadFinished.connect(self.adjustLocation)
  23. self.view.titleChanged.connect(self.adjustTitle)
  24. self.view.loadProgress.connect(self.setProgress)
  25. self.view.loadFinished.connect(self.finishLoading)
  26. self.inspector = QWebInspector()
  27. self.inspector.setPage(self.view.page())
  28. toolBar = self.addToolBar("Navigation")
  29. toolBar.addAction(self.view.pageAction(QWebPage.Back))
  30. toolBar.addAction(self.view.pageAction(QWebPage.Forward))
  31. toolBar.addAction(self.view.pageAction(QWebPage.Reload))
  32. toolBar.addAction(self.view.pageAction(QWebPage.Stop))
  33. self.setCentralWidget(self.view)
  34. def viewSource(self):
  35. accessManager = self.view.page().networkAccessManager()
  36. request = QNetworkRequest(self.view.url())
  37. reply = accessManager.get(request)
  38. reply.finished.connect(self.slotSourceDownloaded)
  39. def slotSourceDownloaded(self):
  40. reply = self.sender()
  41. self.textEdit = QTextEdit()
  42. self.textEdit.setAttribute(Qt.WA_DeleteOnClose)
  43. self.textEdit.show()
  44. self.textEdit.setPlainText(QTextStream(reply).readAll())
  45. self.textEdit.resize(600, 400)
  46. reply.deleteLater()
  47. def adjustLocation(self):
  48. #self.locationEdit.setText(self.view.url().toString())
  49. print("Change url")
  50. print(self.view.url().toString())
  51. #elf.view.load(self.view.url())
  52. def changeLocation(self):
  53. #url = QUrl.fromUserInput(self.locationEdit.text())
  54. self.view.load(url)
  55. self.view.setFocus()
  56. def adjustTitle(self):
  57. if 0 < self.progress < 100:
  58. self.setWindowTitle("%s (%s%%)" % (self.view.title(), self.progress))
  59. else:
  60. self.setWindowTitle(self.view.title())
  61. def setProgress(self, p):
  62. self.progress = p
  63. self.adjustTitle()
  64. def finishLoading(self):
  65. self.progress = 100
  66. self.adjustTitle()
  67. self.view.page().mainFrame().evaluateJavaScript(self.Brython)
  68. def highlightAllLinks(self):
  69. code = """$('a').each(
  70. function () {
  71. $(this).css('background-color', 'yellow')
  72. }
  73. )"""
  74. self.view.page().mainFrame().evaluateJavaScript(code)
  75. def rotateImages(self, invert):
  76. if invert:
  77. code = """
  78. $('img').each(
  79. function () {
  80. $(this).css('-webkit-transition', '-webkit-transform 2s');
  81. $(this).css('-webkit-transform', 'rotate(180deg)')
  82. }
  83. )"""
  84. else:
  85. code = """
  86. $('img').each(
  87. function () {
  88. $(this).css('-webkit-transition', '-webkit-transform 2s');
  89. $(this).css('-webkit-transform', 'rotate(0deg)')
  90. }
  91. )"""
  92. self.view.page().mainFrame().evaluateJavaScript(code)
  93. def removeGifImages(self):
  94. code = "$('[src*=gif]').remove()"
  95. self.view.page().mainFrame().evaluateJavaScript(code)
  96. def removeInlineFrames(self):
  97. code = "$('iframe').remove()"
  98. self.view.page().mainFrame().evaluateJavaScript(code)
  99. def removeObjectElements(self):
  100. code = "$('object').remove()"
  101. self.view.page().mainFrame().evaluateJavaScript(code)
  102. def removeEmbeddedElements(self):
  103. code = "$('embed').remove()"
  104. self.view.page().mainFrame().evaluateJavaScript(code)
  105. if __name__ == '__main__':
  106. import sys
  107. app = QApplication(sys.argv)
  108. if len(sys.argv) > 1:
  109. url = QUrl(sys.argv[1])
  110. else:
  111. url = QUrl('http://localhost:8000')
  112. browser = MainWindow(url)
  113. browser.show()
  114. sys.exit(app.exec_())