1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
   | import PyQt5.QtWidgets as qtw # For widget
import PyQt5.QtGui as qtg # For Mouse event and cursor
import PyQt5.QtCore as qtc # For enumeration
import PyQt5.QtWebEngineWidgets as wew #For web engine view
import PyQt5.QtTest as qtt # For test mouse click
 
import typing
 
class MainWinApp(qtw.QMainWindow):
   """@brief GUI creation"""
   def __init__(self) -> None:
      super().__init__()
 
      #Resize main window
      self.resize(800,400)
 
      #Set web browser view
      self.webbrowser = wew.QWebEngineView()
      self.setCentralWidget(self.webbrowser)
      self.webbrowser.setUrl(qtc.QUrl("https://www.youtube.com/"))
 
      #Set navigation bar
      self.navbar = qtw.QToolBar("NavBar")
      self.addToolBar(self.navbar)
 
      #Set "my action" button in navigation bar
      btn_action = qtw.QAction("my action",self)
      btn_action.setStatusTip("Start action")
      btn_action.triggered.connect(self.MouseAction)
      self.navbar.addAction(btn_action)
 
      #Display window
      self.show()
 
   """@brief Action of navigation bar button my action"""
   def MouseAction(self) -> None:
      #Calcul mouse cursor position for click in the middle of web browser view
      w = self.webbrowser.frameGeometry().width()
      h = self.webbrowser.frameGeometry().height()
      x = self.webbrowser.frameGeometry().x()+ self.frameGeometry().x()
      y = self.webbrowser.frameGeometry().y()+self.frameGeometry().y()
      poscursorx = int(x +w/2)
      poscursory = int(y +h/2)
      print(f"x {poscursorx}, y {poscursory}")
      pos = qtc.QPoint(poscursorx,poscursory)
 
      #Move the mouse cursor to the position of the click (In theory, not necessary for the mouse click to take place. I put this to see where my mouse cursor should normally click.)
      cursor = qtg.QCursor()
      cursor.setPos(poscursorx,poscursory)
 
      #Do mouse click
      Click_mouse(self.webbrowser,pos)
      #qtt.QTest.mouseClick(self,qtc.Qt.MouseButton.LeftButton,qtc.Qt.KeyboardModifier.NoModifier,pos=pos)     
 
"""@brief Do left button mouse click at the position gave in parameter
@param widget Widget targgeted by mouse click
@param pos Position where the mouse click should be do"""
def Click_mouse(widget : qtw.QWidget,pos : qtc.QPoint) -> None:
   #Push left mouse button
   event = qtg.QMouseEvent(qtc.QEvent.Type.MouseButtonPress,pos,qtc.Qt.MouseButton.LeftButton,qtc.Qt.MouseButton.NoButton,qtc.Qt.KeyboardModifier.NoModifier)
   qtc.QCoreApplication.postEvent(widget,event)
 
   #Wait 200 ms
   qtc.QThread.usleep(200000)
 
   #Release left mouse button
   event = qtg.QMouseEvent(qtc.QEvent.Type.MouseButtonRelease,pos,qtc.Qt.MouseButton.LeftButton,qtc.Qt.MouseButton.NoButton,qtc.Qt.KeyboardModifier.NoModifier)
   qtc.QCoreApplication.postEvent(widget,event)
 
#main
if __name__ == "__main__" :
   #Application creation
   app = qtw.QApplication([])
   app.setApplicationName("Youtube Sniffer")
 
   #Window creation
   window = MainWinApp()
 
   #Start application
   app.exec()
 
   #Delete data in web browser
   window.webbrowser.page().profile().cookieStore().deleteAllCookies() | 
Partager