-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewHTMLsourcewithJavaScriptandCSSstyles.py
224 lines (193 loc) · 6.99 KB
/
ViewHTMLsourcewithJavaScriptandCSSstyles.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# -*- mode: Python ; coding: utf-8 -*-
# • View HTML source with JavaScript and CSS styles
# https://ankiweb.net/shared/info/1128123950
# https://github.com/ankitest/anki-musthave-addons-by-ankitest
# -- tested with Anki 2.0.44 under Windows 7 SP1
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
# Copyright (c) 2016-2017 Dmitry Mikheev, http://finpapa.ucoz.net/
# No support. Use it AS IS on your own risk.
"""
Menu Cards - View Source code Body Alt+F3
shows HTML source with JavaScript and CSS styles
but without jQuery Menu Cards
Menu Cards - View Source code Ctrl+F3
shows full HTML source
Works for decks panel, deck overview and any card.
"""
from __future__ import division
from __future__ import unicode_literals
import os
import sys
import anki
import aqt
from PyQt4.QtGui import *
from PyQt4.QtCore import *
FONTS = {
'showText': ('Calibri', 16),
}
HOTKEY = { # in aqt.mw Main Window (deckBrowser, Overview, Reviewer)
'HTML_source': 'Ctrl+F3',
'Body_source': 'Alt+F3',
}
# Get language class
# import anki.lang
lang = anki.lang.getLang()
MSG = {
'en': {
'later': _('later'),
'not now': _('not now'),
'Cards': _('&Cards'),
'no_jQuery': _('View Source code &Body'),
'view_source': _('&View Source code'),
'toolbarWeb': _('View toolbar Source'),
'bottomWeb': _('View bottomWeb Source'),
},
'ru': {
'later': 'позже',
'not now': 'не сейчас',
'Cards': '&Карточки',
'no_jQuery': 'Показать Ис&ходник HTML Body',
'view_source': 'Показать И&сходник HTML',
'toolbarWeb': 'Показать Верхний Исходник',
'bottomWeb': 'Показать Нижний Исходник',
},
}
try:
MSG[lang]
except KeyError:
lang = 'en'
try:
MUSTHAVE_COLOR_ICONS = os.path.join(
aqt.mw.pm.addonFolder(), 'handbook')
except:
MUSTHAVE_COLOR_ICONS = ''
# 'Показать Ис&ходник HTML Body' if lang == 'ru' else 'View Source code &Body'
# 'Показать И&сходник HTML' if lang == 'ru' else '&View Source code'
__addon__ = "'" + __name__.replace('_', ' ')
__version__ = "2.0.44b"
try:
aqt.mw.addon_cards_menu
except AttributeError:
aqt.mw.addon_cards_menu = QMenu(MSG[lang]['Cards'], aqt.mw)
aqt.mw.form.menubar.insertMenu(
aqt.mw.form.menuTools.menuAction(), aqt.mw.addon_cards_menu)
def particularFont(fontKey, bold=False, italic=False, underline=False):
font = QFont()
if fontKey in FONTS:
if FONTS[fontKey][0] is not None:
font.setFamily(FONTS[fontKey][0])
fontsize = int(FONTS[fontKey][1])
if fontsize > 0:
font.setPixelSize(fontsize)
# font.setPointSize(fontsize)
font.setBold(bold)
font.setItalic(italic)
font.setUnderline(underline)
return font
def showTextik(txt, parent=None, type="text", run=True, geomKey=None, \
minWidth=500, minHeight=400, title="Anki"):
if not parent:
parent = aqt.mw.app.activeWindow() or aqt.mw
diag = QDialog(parent)
diag.setWindowTitle(title)
layout = QVBoxLayout(diag)
diag.setLayout(layout)
text = QTextEdit()
text.setReadOnly(True)
text.setFont(particularFont('showText'))
if type == "text":
text.setPlainText(txt)
else:
text.setHtml(txt)
layout.addWidget(text)
box = QDialogButtonBox(QDialogButtonBox.Close)
layout.addWidget(box)
def onReject():
if geomKey:
aqt.utils.saveGeom(diag, geomKey)
QDialog.reject(diag)
diag.connect(box, SIGNAL("rejected()"), onReject)
# box.rejected.connect(onReject)
# Python 3
def onFinish():
if geomKey:
aqt.utils.saveGeom(diag, geomKey)
diag.connect(box, SIGNAL("rejected()"), onReject)
# box.accepted.connect(onFinish)
# Python 3
diag.setMinimumHeight(minHeight)
diag.setMinimumWidth(minWidth)
if geomKey:
aqt.utils.restoreGeom(diag, geomKey)
if run:
diag.exec_()
else:
return diag, box
aqt.utils.showText = showTextik
def _getSourceHTML():
"""To look at sourcne HTML+CSS code."""
html = aqt.mw.web.page().mainFrame().evaluateJavaScript("""
(function(){
return document.documentElement.outerHTML
}())
""")
aqt.utils.showText(html, geomKey="ViewHTML")
def _getSourceBody(mWeb):
"""To look at sourcne HTML+CSS code."""
html = '<html class="' +\
unicode(mWeb.page().mainFrame().evaluateJavaScript("""
(function(){
return document.documentElement.className
}())
""")) + '">\n<head>\n' +\
unicode(
mWeb.page().mainFrame().evaluateJavaScript(
"""
(function(){
return document.getElementsByTagName('head')[0]""" +
""".getElementsByTagName('style')[0].outerHTML
}())
""")) + '\n</head>\n' +\
unicode(mWeb.page().mainFrame().evaluateJavaScript("""
(function(){
return document.body.outerHTML
}())
""")) + '\n</html>\n'
aqt.utils.showText(html, geomKey="ViewHTML")
get_Body_Source_action = QAction(aqt.mw)
get_Body_Source_action.setText(MSG[lang]['no_jQuery'])
get_Body_Source_action.setShortcut(
QKeySequence(HOTKEY['Body_source']))
get_Body_Source_action.setIcon(
QIcon(os.path.join(MUSTHAVE_COLOR_ICONS, 'html.png')))
aqt.mw.connect(
get_Body_Source_action, SIGNAL('triggered()'),
lambda: _getSourceBody(aqt.mw.web))
get_HTML_Source_action = QAction(aqt.mw)
get_HTML_Source_action.setText(MSG[lang]['view_source'])
get_HTML_Source_action.setShortcut(
QKeySequence(HOTKEY['HTML_source']))
get_HTML_Source_action.setIcon(
QIcon(os.path.join(MUSTHAVE_COLOR_ICONS, 'html5.png')))
aqt.mw.connect(
get_HTML_Source_action, SIGNAL('triggered()'), _getSourceHTML)
get_Top_Source_action = QAction(aqt.mw)
get_Top_Source_action.setText(MSG[lang]['toolbarWeb'])
aqt.mw.connect(
get_Top_Source_action, SIGNAL('triggered()'),
lambda: _getSourceBody(aqt.mw.toolbar.web))
get_Bottom_Source_action = QAction(aqt.mw)
get_Bottom_Source_action.setText(MSG[lang]['bottomWeb'])
aqt.mw.connect(
get_Bottom_Source_action, SIGNAL('triggered()'),
lambda: _getSourceBody(aqt.mw.bottomWeb))
# -- these work perfectly well
# aqt.mw.toolbar.web.hide()
# aqt.mw.toolbar.web.setFixedHeight(50)
if hasattr(aqt.mw, 'addon_cards_menu'):
aqt.mw.addon_cards_menu.addSeparator()
aqt.mw.addon_cards_menu.addAction(get_Top_Source_action)
aqt.mw.addon_cards_menu.addAction(get_Body_Source_action)
aqt.mw.addon_cards_menu.addAction(get_HTML_Source_action)
aqt.mw.addon_cards_menu.addAction(get_Bottom_Source_action)
aqt.mw.addon_cards_menu.addSeparator()