Bug 85827 - [Qt] [WK2] Allow user to inject JS scripts when the page loads
Summary: [Qt] [WK2] Allow user to inject JS scripts when the page loads
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebKit Qt (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Caio Marcelo de Oliveira Filho
URL:
Keywords: Qt, QtTriaged
Depends on: 87731
Blocks:
  Show dependency treegraph
 
Reported: 2012-05-07 14:12 PDT by Caio Marcelo de Oliveira Filho
Modified: 2012-05-29 04:44 PDT (History)
8 users (show)

See Also:


Attachments
Patch (18.78 KB, patch)
2012-05-07 14:41 PDT, Caio Marcelo de Oliveira Filho
no flags Details | Formatted Diff | Diff
Patch (30.41 KB, patch)
2012-05-11 12:34 PDT, Caio Marcelo de Oliveira Filho
no flags Details | Formatted Diff | Diff
Patch (30.33 KB, patch)
2012-05-21 06:56 PDT, Caio Marcelo de Oliveira Filho
hausmann: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Caio Marcelo de Oliveira Filho 2012-05-07 14:12:20 PDT
The user of WebView can specify a list of JS scripts that will be loaded in the JS environment of the page. This allow the WebView user to manipulate the DOM. The WebView user can also take advantage of the existing (experimental) messaging feature to communicate between the injected script (that has access to the DOM) and its app environment (usually in QML).

This is related to bug 75099, but with two important simplifications: we inject the scripts in the web page JS environment instead of allowing creation of isolated JS worlds, and the API is simpler.

This feature together with messaging can cover many cases previously handled by manipulating the page via QWebElement directly.
Comment 1 Caio Marcelo de Oliveira Filho 2012-05-07 14:41:12 PDT
Created attachment 140595 [details]
Patch
Comment 2 Noam Rosenthal 2012-05-09 06:27:19 PDT
Comment on attachment 140595 [details]
Patch

I think this is just right from a functionality standpoint.
But since it's new API I'd rather some of the API-folks help review it.
Comment 3 Tor Arne Vestbø 2012-05-09 06:41:49 PDT
Nice! Though I think userScript is a better name.
Comment 4 Noam Rosenthal 2012-05-09 08:50:28 PDT
Comment on attachment 140595 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=140595&action=review

> Source/WebKit2/WebProcess/qt/QtBuiltinBundlePage.cpp:183
> +        if (source.startsWith(QLatin1String("file:///")))
> +            source = QUrl(source).toLocalFile();

It's better to use QUrl::isLocalFile rather than test for startsWith.
Also, using resources (qrc and the likes) for this is pretty popular so we should think about that.
Comment 5 Noam Rosenthal 2012-05-09 09:08:28 PDT
Comment on attachment 140595 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=140595&action=review

> Source/WebKit2/WebProcess/qt/QtBuiltinBundlePage.cpp:182
> +        if (source.startsWith(QLatin1String("file:///")))

I think we should only allow file:, data: and qrc, and not try to open the URL otherwise.
Comment 6 Caio Marcelo de Oliveira Filho 2012-05-09 13:48:13 PDT
Comment on attachment 140595 [details]
Patch

Thanks for the feedback. I'm working on a new version of the patch.
Comment 7 Caio Marcelo de Oliveira Filho 2012-05-11 12:34:54 PDT
Created attachment 141470 [details]
Patch
Comment 8 Caio Marcelo de Oliveira Filho 2012-05-11 12:35:50 PDT
(In reply to comment #7)
> Created an attachment (id=141470) [details]
> Patch

This updated version adds:

- Resource file support.
- More test cases.
Comment 9 Noam Rosenthal 2012-05-11 12:55:40 PDT
This looks good to me, but I'd rather more people take a look!
Comment 10 Tor Arne Vestbø 2012-05-11 16:30:08 PDT
Very cool, I like it! 

Would be nice to proto if we're able to run GM-scripts like this one:

  http://userscripts.org/scripts/review/109262

if we do 

  userScripts: ["GM-functions.js", "109262.js"], 

where the former would use the messaging feature to implement (some of) the GM apis:

  http://wiki.greasespot.net/Greasemonkey_Manual:API
Comment 11 Tor Arne Vestbø 2012-05-11 16:39:41 PDT
(In reply to comment #10)
> Would be nice to proto if we're able to run GM-scripts like this one:

Ah, never mind, I see bug 75099 is about enabling something like GM-scripts, with proper isolation, etc.
Comment 12 Simon Hausmann 2012-05-17 23:42:27 PDT
Comment on attachment 141470 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=141470&action=review

A few comments. I like the re-use of existing functionality :)

> Source/WebKit2/UIProcess/API/qt/qquickwebview.cpp:729
> +        bool ok = false;
> +        QString contents = readUserScript(url, ok);
> +        if (!ok)
> +            continue;
> +        scripts.append(String(contents));

Wouldn't it be simpler to just return an empty string instead of the bool ok out parameter? An empty string isn't really worth appending to "scripts" I think :)

> Source/WebKit2/UIProcess/API/qt/qquickwebview_p.h:270
> +    Q_PROPERTY(QList<QUrl> userScripts READ userScripts WRITE setUserScripts NOTIFY userScriptsChanged)

Is QList the correct type? Shouldn't this be implemented as a QML list property?
Comment 13 Caio Marcelo de Oliveira Filho 2012-05-21 06:56:07 PDT
Created attachment 143024 [details]
Patch
Comment 14 Caio Marcelo de Oliveira Filho 2012-05-21 06:57:37 PDT
Comment on attachment 141470 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=141470&action=review

>> Source/WebKit2/UIProcess/API/qt/qquickwebview.cpp:729
>> +        scripts.append(String(contents));
> 
> Wouldn't it be simpler to just return an empty string instead of the bool ok out parameter? An empty string isn't really worth appending to "scripts" I think :)

Fixed, also added a warning when this case happens.

>> Source/WebKit2/UIProcess/API/qt/qquickwebview_p.h:270
>> +    Q_PROPERTY(QList<QUrl> userScripts READ userScripts WRITE setUserScripts NOTIFY userScriptsChanged)
> 
> Is QList the correct type? Shouldn't this be implemented as a QML list property?

QQmlListProperty can only be used with lists of QObject pointers http://doc-snapshot.qt-project.org/5.0/qqmllistproperty.html. We use QUrl here as a value type, and it have implicit sharing happening in the backstage. This usage should be as fine as QStringList :-)
Comment 15 Caio Marcelo de Oliveira Filho 2012-05-29 04:22:58 PDT
Committed r118752: <http://trac.webkit.org/changeset/118752>
Comment 16 WebKit Review Bot 2012-05-29 04:34:15 PDT
Re-opened since this is blocked by 87731
Comment 17 Caio Marcelo de Oliveira Filho 2012-05-29 04:44:41 PDT
Committed r118756: <http://trac.webkit.org/changeset/118756>