-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from ccordoba12/dom-widget
Add a widget to do DOM manipulations using Javascript
- Loading branch information
Showing
3 changed files
with
66 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) Spyder Project Contributors | ||
# Licensed under the terms of the MIT License | ||
|
||
""" | ||
Widget to do DOM manipulations using Javascript | ||
Some code here comes from the Ghost.py project: | ||
https://github.com/jeanphix/Ghost.py | ||
""" | ||
|
||
from qtpy.QtWebEngineWidgets import WEBENGINE | ||
from spyder.widgets.browser import WebView | ||
|
||
|
||
class DOMWidget(WebView): | ||
|
||
def __init__(self, parent): | ||
super(DOMWidget, self).__init__(parent) | ||
if WEBENGINE: | ||
self.page = self.page() | ||
else: | ||
self.page = self.page().mainFrame() | ||
|
||
def evaluate(self, script): | ||
"""Evaluates script in page frame. | ||
:param script: The script to evaluate. | ||
""" | ||
if WEBENGINE: | ||
return self.page.runJavaScript("%s" % script) | ||
else: | ||
return self.page.evaluateJavaScript("%s" % script) | ||
|
||
def click(self, selector, btn=0): | ||
"""Click the targeted element. | ||
:param selector: A CSS3 selector to targeted element. | ||
:param btn: The number of mouse button. | ||
0 - left button, | ||
1 - middle button, | ||
2 - right button | ||
""" | ||
return self.evaluate(""" | ||
(function () { | ||
var element = document.querySelector(%s); | ||
var evt = document.createEvent("MouseEvents"); | ||
evt.initMouseEvent("click", true, true, window, 1, 1, 1, 1, 1, | ||
false, false, false, false, %s, element); | ||
return element.dispatchEvent(evt); | ||
})(); | ||
""" % (repr(selector), str(btn))) | ||
|
||
def set_input_value(self, selector, value): | ||
"""Sets the value of the input matched by given selector.""" | ||
script = 'document.querySelector("%s").setAttribute("value", "%s")' | ||
script = script % (selector, value) | ||
self.evaluate(script) |