webenginescript.cpp 9.69 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
  Copyright (c) 2016 Montel Laurent <montel@kde.org>

  This program is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License, version 2, as
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
#include "webenginescript.h"
18
#include <QDebug>
19

20
21
22
using namespace MessageViewer;

QString WebEngineScript::findAllImages()
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{
    const QString source = QLatin1String("(function() {"
                                         "var out = [];"
                                         "var imgs = document.getElementsByTagName('img');"
                                         "for (var i = 0; i < imgs.length; ++i) {"
                                         "    var e = imgs[i];"
                                         "    out.push({"
                                         "        src: e.src"
                                         "    });"
                                         "}"
                                         "return out;"
                                         "})()");

    return source;
}

39
QString WebEngineScript::findAllScripts()
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
{
    const QString source = QLatin1String("(function() {"
                                         "var out = [];"
                                         "var scripts = document.getElementsByTagName('script');"
                                         "for (var i = 0; i < scripts.length; ++i) {"
                                         "    var e = scripts[i];"
                                         "    out.push({"
                                         "        src: e.src"
                                         "    });"
                                         "}"
                                         "return out;"
                                         "})()");

    return source;
}
55
56
57
58
59

QString WebEngineScript::findAllAnchors()
{
    const QString source = QLatin1String("(function() {"
                                         "var out = [];"
Laurent Montel's avatar
Laurent Montel committed
60
61
                                         "var anchors = document.getElementsByTagName('a');"
                                         "for (var i = 0; i < anchors.length; ++i) {"
Laurent Montel's avatar
Laurent Montel committed
62
                                         "    var r = anchors[i].getBoundingClientRect();"
63
                                         "    out.push({"
Laurent Montel's avatar
Laurent Montel committed
64
                                         "        src: anchors[i].href,"
Laurent Montel's avatar
Laurent Montel committed
65
66
                                         "        title: anchors[i].title,"
                                         "        boudingRect: [r.top, r.left, r.width, r.height]"
67
68
69
70
71
72
73
                                         "    });"
                                         "}"
                                         "return out;"
                                         "})()");

    return source;
}
Laurent Montel's avatar
Laurent Montel committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

QString WebEngineScript::findAllAnchorsAndForms()
{
    const QString source = QLatin1String("(function() {"
                                         "var res = [];"
                                         "var out = [];"
                                         "var anchors = document.getElementsByTagName('a');"
                                         "for (var i = 0; i < anchors.length; ++i) {"
                                         "    out.push({"
                                         "        src: anchors[i].href,"
                                         "        title: anchors[i].title"
                                         "    });"
                                         "}"
                                         "var forms = document.getElementsByTagName('form');"
                                         "res.push({"
                                         "    anchors: out,"
                                         "    forms:  forms.length"
                                         "    });"
                                         "return res;"
                                         "})()");

    return source;
}
97

Laurent Montel's avatar
Laurent Montel committed
98
99
100
101
QString WebEngineScript::setElementByIdVisible(const QString &elementStr, bool visibility)
{
    if (visibility) {
        const QString source = QString::fromLatin1("var element = document.getElementById('%1'); "
Laurent Montel's avatar
Laurent Montel committed
102
103
104
                               "if (element) { "
                               "    element.style.removeProperty( 'display' );"
                               "}").arg(elementStr);
Laurent Montel's avatar
Laurent Montel committed
105
106
107
        return source;
    } else {
        const QString source = QString::fromLatin1("var element = document.getElementById('%1'); "
Laurent Montel's avatar
Laurent Montel committed
108
109
110
                               "if (element) { "
                               "    element.style.display = \"none\";"
                               "}").arg(elementStr);
Laurent Montel's avatar
Laurent Montel committed
111
112
113
114
        return source;
    }
}

115
116
QString WebEngineScript::searchElementPosition(const QString &elementStr)
{
Laurent Montel's avatar
Laurent Montel committed
117
    const QString source = QString::fromLatin1("var element = document.getElementById('%1'); "
Laurent Montel's avatar
Laurent Montel committed
118
119
120
121
                           "if (element) { "
                           "    var geometry = element.getBoundingClientRect(); "
                           "    [(geometry.left + window.scrollX), (geometry.top + window.scrollY)]; "
                           "}").arg(elementStr);
Laurent Montel's avatar
Laurent Montel committed
122
    //qDebug()<<" source "<<source;
123
124
    return source;
}
125

126
127
128
129
130
131
QString WebEngineScript::scrollPercentage(int percent)
{
    //TODO
    return QString();
}

Laurent Montel's avatar
Laurent Montel committed
132
133
QString WebEngineScript::scrollUp(int pixel)
{
134
    const QString source = QString::fromLatin1("window.scrollBy(0, %1);").arg(-pixel);
Laurent Montel's avatar
Laurent Montel committed
135
136
137
138
139
    return source;
}

QString WebEngineScript::scrollDown(int pixel)
{
140
    const QString source = QString::fromLatin1("window.scrollBy(0, %1);").arg(pixel);
Laurent Montel's avatar
Laurent Montel committed
141
142
143
    return source;
}

144
145
146
QString WebEngineScript::scrollToPosition(const QPoint &pos)
{
    const QString source = QString::fromLatin1("window.scrollTo(%1, %2); [window.scrollX, window.scrollY];").arg(pos.x()).arg(pos.y());
Laurent Montel's avatar
Laurent Montel committed
147
    //qDebug()<<" source "<<source;
148
149
    return source;
}
Laurent Montel's avatar
Laurent Montel committed
150

Laurent Montel's avatar
Laurent Montel committed
151
QString WebEngineScript::removeStyleToElement(const QString &elementStr)
152
{
Laurent Montel's avatar
Laurent Montel committed
153
154
155
156
157
    const QString source = QString::fromLatin1("var element = document.getElementById('%1'); "
                                               "if (element) { "
                                               "    element.removeAttribute(\"style\");"
                                               "}").arg(elementStr);
    return source;
158
159
}

Laurent Montel's avatar
Laurent Montel committed
160
161
162
QString WebEngineScript::setStyleToElement(const QString &elementStr, const QString &style)
{
    const QString source = QString::fromLatin1("var element = document.getElementById('%1'); "
Laurent Montel's avatar
Laurent Montel committed
163
164
165
                           "if (element) { "
                           "    element.style = '%2';"
                           "}").arg(elementStr).arg(style);
Laurent Montel's avatar
Laurent Montel committed
166
167
    return source;
}
Laurent Montel's avatar
Laurent Montel committed
168
169
170
171
172
173
174

QString WebEngineScript::scrollToRelativePosition(int pos)
{
    //TODO verify it
    const QString source = QString::fromLatin1("window.scrollTo(window.scrollX, %1); [window.scrollX, window.scrollY];").arg(pos);
    return source;
}
175

Laurent Montel's avatar
Laurent Montel committed
176
177
178
179
QString WebEngineScript::injectAttachments(const QString &delayedHtml, const QString &elementStr)
{
    const QString source = QString::fromLatin1("var element = document.getElementById('%1'); "
                           "if (element) { "
Laurent Montel's avatar
Laurent Montel committed
180
                           "    element.innerHTML += '%2';"
Laurent Montel's avatar
Laurent Montel committed
181
                           "}").arg(elementStr).arg(delayedHtml);
Laurent Montel's avatar
Laurent Montel committed
182
    qDebug() << "QString WebEngineScript::injectAttachments(const QString &delayedHtml, const QString &elementStr) :"<<source;
Laurent Montel's avatar
Laurent Montel committed
183
184
    return source;
}
Laurent Montel's avatar
Laurent Montel committed
185
186
187

QString WebEngineScript::toggleFullAddressList(const QString &field, const QString &html, bool doShow)
{
Laurent Montel's avatar
Laurent Montel committed
188
189
190
191
192
193
194
195
196
197
198
199
200
    const QString replaceInnerHtmlStr = QLatin1String("iconFull") + field + QLatin1String("AddressList");
    const QString dotsFullStr = QLatin1String("dotsFull")   + field + QLatin1String("AddressList");
    const QString hiddenFullStr = QLatin1String("hiddenFull") + field + QLatin1String("AddressList");
    const QString source = QString::fromLatin1("var element = document.getElementById('%1'); "
                                               "if (element) { "
                                               "    element.innerHTML += '%2';"
                                               "    %3;"
                                               "    %4;"
                                               "}").arg(replaceInnerHtmlStr).arg(html)
            .arg(MessageViewer::WebEngineScript::setElementByIdVisible(dotsFullStr, !doShow))
            .arg(MessageViewer::WebEngineScript::setElementByIdVisible(hiddenFullStr, doShow));
    qDebug() << "QString WebEngineScript::injectAttachments(const QString &delayedHtml, const QString &elementStr) :"<<source;
    return source;
Laurent Montel's avatar
Laurent Montel committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#if 0
    if (mViewer->replaceInnerHtml(QLatin1String("iconFull") + field + QLatin1String("AddressList"),
                                  bind(&ViewerPrivate::recipientsQuickListLinkHtml, this, doShow, field))) {
        // Then show/hide the full address list
        mViewer->setElementByIdVisible(QLatin1String("dotsFull")   + field + QLatin1String("AddressList"), !doShow);
        mViewer->setElementByIdVisible(QLatin1String("hiddenFull") + field + QLatin1String("AddressList"),  doShow);
    }
    qDebug() << "bool MailWebEngineView::replaceInnerHtml(const QString &id, const boost::function<QString()> &delayedHtml)";
    QWebElement doc = page()->currentFrame()->documentElement();
    QWebElement tag = doc.findFirst(QLatin1String("*#") + id);
    if (tag.isNull()) {
        return false;
    }
    tag.setInnerXml(delayedHtml());
    return true;
#endif
    //TODO
    return {};
}