Skip to content

Commit

Permalink
Merge pull request #24 from ccordoba12/dom-widget
Browse files Browse the repository at this point in the history
Add a widget to do DOM manipulations using Javascript
  • Loading branch information
ccordoba12 authored Jan 14, 2017
2 parents 5060f5a + d5032d7 commit 1e6d578
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
3 changes: 1 addition & 2 deletions spyder_notebook/notebookplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from spyder.utils.qthelpers import create_action, create_toolbutton
from spyder.widgets.tabs import Tabs
from spyder.plugins import SpyderPluginWidget
from spyder.py3compat import to_text_string

# Local imports
from .utils.nbopen import nbopen, NBServerError
Expand Down Expand Up @@ -146,7 +145,7 @@ def register_plugin(self):
#------ Public API (for clients) ------------------------------------------
def get_clients(self):
"""Return notebooks list"""
return [cl for cl in self.clients if isinstance(nb, NotebookClient)]
return [cl for cl in self.clients if isinstance(cl, NotebookClient)]

def get_focus_client(self):
"""Return current notebook with focus, if any"""
Expand Down
10 changes: 8 additions & 2 deletions spyder_notebook/widgets/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@
from spyder.py3compat import is_text_string
from spyder.utils.qthelpers import add_actions
from spyder.utils import sourcecode
from spyder.widgets.browser import WebView
from spyder.widgets.findreplace import FindReplace

# Local imports
from ..widgets.dom import DOMWidget


#-----------------------------------------------------------------------------
# Templates
Expand All @@ -45,7 +47,7 @@
#-----------------------------------------------------------------------------
# Widgets
#-----------------------------------------------------------------------------
class NotebookWidget(WebView):
class NotebookWidget(DOMWidget):
"""WebView widget for notebooks."""

def contextMenuEvent(self, event):
Expand Down Expand Up @@ -159,6 +161,10 @@ def get_short_name(self):
sname = osp.basename(self.name)
return sname

def save(self):
"""Save current notebook."""
self.notebookwidget.click('#save-notbook button')


#-----------------------------------------------------------------------------
# Tests
Expand Down
57 changes: 57 additions & 0 deletions spyder_notebook/widgets/dom.py
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)

0 comments on commit 1e6d578

Please sign in to comment.