Commit Graph

14300 Commits

Author SHA1 Message Date
Jimmy
0381d74e9a Greasemonkey: privatise some utility functions 2017-11-27 20:10:38 +13:00
Florian Bruhin
0e80be2d30 Clear end2end test data again after initializing
If we don't do this, earlier tests can affect later ones when e.g. using "...
should not be logged", as we don't really wait until a test has been fully
finished.
2017-11-27 20:10:38 +13:00
Florian Bruhin
db353c4030 Connect the page signal for GreaseMonkey
Looks like we don't get the mainFrame's loadFinished signal properly.
2017-11-27 20:10:38 +13:00
Florian Bruhin
6933bc05b4 Add some debug logging for GreaseMonkey with QtWebKit 2017-11-27 20:10:38 +13:00
Jimmy
df624944f9 Greasemonkey: webkit: injected all scripts on loadFinished.
The signal we were using to inject greasemonkey scripts registered to
run at document-start (javaScriptWindowObjectCleared) was unreliable to
non-existant. The initialLayoutCompleted signal is a bit of an odd duck
too I suppose. Anyway, we don't anticipate any scripts would break from
being injected when the page is finished loaded that wouldn't already
have been flaky due to the complexities of the modern web. If there is
an issue hopefully someone raises an issue and we can look into it.
2017-11-27 20:10:38 +13:00
Jimmy
8a5b42ffbd Greasemonkey: es6ify the greasemonkey wrapper js.
Because backwards compatibility sucks I guess.
2017-11-27 20:10:38 +13:00
Jimmy
92b48e77c7 Greasemonkey: add unit tests for GreasemonkeyManager 2017-11-27 20:10:38 +13:00
Jimmy
dd59f8d724 Greasemonkey: add more end2end tests
Test document-end and noframes. Because coverage.py told me to.
Hopefully this doesn't slow the test run down too much, particularly the
"should not be logged" bit.

I'm just reusing and existing test html page that used an iframe because
I'm lazy.
2017-11-27 20:10:38 +13:00
Jimmy
361a1ed6e4 Greasemonkey: change PROPS_REGEX to handle non-value keys.
We weren't actually picking up the @noframes greasemonkey directive
because of this. I haven't tested this very extensively but it seems to
work for making the property value optional.
2017-11-27 19:27:31 +13:00
Jimmy
9aeb5775c1 greasemonkey: run scripts on subframes on webkit
Use the `QWebPage.frameCreated` signal to get notifications of subframes
and connect the javascript injection triggering signals on those frames
too.

I had to add a `url = url() or requestedUrl()` bit in there because the
inject_userjs method was getting called to early or something when
frame.url() wasn't set or was set to the previous page so we were
passing the wrong url to greasemonkey.scripts_for().

I ran into a bizarre (I maybe it is completely obvious and I just don't
see it) issue where the signals attached to the main frame that were
connected to a partial function with the main frame as an argument were
not getting emitted, or at least those partial functions were not being
called. I worked around it by using None to mean defaulting to the main
frame in a couple of places.
2017-11-27 19:27:31 +13:00
Jimmy
4c3461038d Greasemonkey: add minimal end-to-end test.
Just runs a greasemonkey script on a test page and uses console.log to
ensure it is running.

Tests @include, and basic happy path greasemonkey.py operation (loading
and parsing script, scrip_for on webkit), only testing document-start
injecting point but that is the troublsome one at this point.

Tested on py35 debian unstable (oldwebkit and qtwebengine5.9) debian
stable qtwebengine5.7.

Note the extra :reload call for qt5.7 because document-start scripts
don't seem to run on the first page load with the current insertion
point. I need to look into this more to look at ways of fixing this.
2017-11-27 19:27:31 +13:00
Jimmy
7c497427ce Greasemonkey: various javascript fixups to GM wrapper template.
Thanks to @sandrosc. A few breaking changes fixed (default method to
GM_xhr not working, GM_listvalues not cleaning up output, GM_setvalue
param checking logic wrong) and a few hygenic changes made.
2017-11-27 19:27:31 +13:00
Jimmy
c0832eb04b Greasemonkey: support @nosubframes.
And run on frames by default. At least on webengine. There is probably
some api to enumerate frames on a webkit page.

Not tested.
2017-11-27 19:27:31 +13:00
Jimmy
fb019b2dab Address second round line comments.
Add qute version to GM_info object in GM wrapper.
Support using the greasemonkey @namespace metadata for its intended
purpose of avoiding name collisions.
Get a nice utf8 encoded string from a QUrl more better.
2017-11-27 19:27:31 +13:00
Jimmy
efde31aa57 Greasemonkey: Support QTWebEngine versions < 5.8
QTWebEngine 5.8 added support for parsing greasemonkey metadata blocks
and scripts added to the QWebEngineScriptCollection of a page or its
profile and then deciding what urls to run those scripts on and at what
point in the load process to run them. For earlier versions we must do
that work ourselves. But with the additional handicap of the less rich
qtwebengine api.

We have acceptNavigationRequest, loadStarted, loadProgress,
loadFinished, urlChanged to choose from regarding points at which to
register scripts for the current page.

Adding scripts on acceptNavigation loadStarted and loadFinished causes
scripts to run too early or too late (eg on the pages being navigated
from/to) and not run on the desired page at the time they are inserted.
We could maybe do some more sophisticated stuff with loadProgress but it
didn't have any better behaviour in the brief testing I gave it.
Registering scripts on the urlChanged event seems to work fine. Even if
it seems like there could be problems with the signal firing too often,
due to not necessarily being tied to the page load progress, that
doesn't seem to have an effect in practice. The event is fired when, for
example, the url fragment changes and even if we add a new script to the
collection (or remove an existing one) it doesn't have an effect on what
is running on the page.

I suspect all of those timing issues is due to the signals being
forwarded fairly directly from the underlying chomium/blink code but the
webengine script stuff only being pushed back to the implementation on
certain events.

Anyway, using urlChanged seems to work fine due to some quirk(s) of the
implementation. That might change with later development but this
codepath is only ever going to be used for version 5.7.

There are other potential optimizations like not removing and then
re-adding scripts for the current page. But they probably wouldn't do
anything anyway, or at least anything that you would expect.
2017-11-27 19:27:31 +13:00
Jimmy
209e43e0ba Greasemonkey: Match against percent encoded urls only.
This change requires urls specified in @include, @exclude and @matches
directives in metadata blocks to be in the same form that
QUrl.toEncoded() returns. That is a punycoded domain and percent encoded
path and query. This seems to be what Tampermonkey on chrome expects to.

Also changes the scripts_for() function to take a QUrl arg so the caller
doesn't need to worry about encodings.
2017-11-27 19:27:31 +13:00
Jimmy
d318178567 Greasemonkey: Fix metadata block regex.
This regex was broken since the original PR and subsequent code seemed to be
working around it. Before re.split was returning [everything up to
/UserScript, everything else], now it returns [before UserScript, metadata,
after /UserScript], which is good.

Also I added the check for the UserScript line starting at column 0 as per
spec.
2017-11-27 19:27:31 +13:00
Jimmy
5e49e7eef2 Greasemonkey: Throw Errors if GM_ function args wrong type.
These argument type restrictions are mentioned on the greasespot pages for
these value storage functions. We could call JSON.dumps() instead but better
to push that onto the caller so we don't have to try handle deserialization.

Also removes the check for localstorage because everyone has supported that
for years.
2017-11-27 19:27:31 +13:00
Jimmy
d93c583c0d Greasemonkey: Escape jinja variables for JS strings. 2017-11-27 19:27:31 +13:00
Jimmy
a7f41b4564 Greasemonkey: ensure only GM scripts are cleaned up on reload.
WebEngine only. Previously we were just removing every script from the
main world. But some other scripts might got here in the future so new
we are overriding the name field to add a GM- prefix so hopefully we
only remove greasemonkey scripts before adding new ones.
2017-11-27 19:27:31 +13:00
Jimmy
fd5d44182b Greasemonkey: move GM_* template into seperate file.
Also ported it to jinja rather than str.format().

Also ran the js through jslint and fixed up a few very minor things.
2017-11-27 19:27:31 +13:00
Jimmy
c1b912f567 Greasemonkey: move inject_userscripts into webenginesettings 2017-11-27 19:27:31 +13:00
Jimmy
edf737ff7d Greasemonkey: move scripts for a domain into data class.
Also makes scripts that don't include a greasemonkey metadata block
match any url. QWebEngine already has that behaviour.
2017-11-27 19:27:31 +13:00
Jimmy
41035cb5ca Greasemonkey: restrict page schemes that scripts can run on
Scripts shouldn't run on qute://settings or source:// etc.

Whitelist from:
https://wiki.greasespot.net/Include_and_exclude_rules
2017-11-27 19:27:31 +13:00
Jimmy
799730f686 Remove GM_ and userscript variables from global scope. 2017-11-27 19:27:31 +13:00
Jimmy
325c595b89 Greasemonkey: Don't strip gm metadata from scripts when loading.
Since we just pass them to webenginescriptcollection on that backend and
that wants to parse it itself to figure out injection point etc.
2017-11-27 19:27:31 +13:00
Jimmy
f26377351c Greasemonkey: Add greasemonkey hooks for webengine.
For qtwebengine 5.8+ only. This is because as of 5.8 some greasemonkey
script support is in upstream. That is, qtwebenginescript(collection)
parses the greasemonkey metadata block and uses @include/match/exclude
to decide what sites to inject a script onto and @run-at to decide when
to inject it, which saves us the trouble. Notes on doing this in <5.8
are below.

Scripts are currently injected into the main "world", that is the same
world as the javascript from the page. This is good because it means
userscripts can modify more stuff on the page but it would be nice if we
could have more isolation without sacrificing functionality. I'm still
looking into why my more feature-full scripts are not having any effect
on the page while running in a separate world.

Userscripts are added to both the default and private profile because I
that if people have scripts installed they want them to run in private mode
too.

We are grabbing the scripts from the greasemonkey module, as opposed to
reading them directly from disk, because the module adds some GM_* functions
that the scripts may expect, and because that is used for webkit anyway.

I have code to support qtwebengine <5.8 but didn't because I am not
happy with the timing of some of the signals that we are provided
regarding page load state, and the actual load state. While the
difference between document-end and document-idle isn't so bad,
injecting document-start scripts when a urlChanged event is emitted
results in the script being injected into the environment for the page
being navigated away from. Anyway, if anyone wants this for earlier
webengines I can oblige them.
2017-11-27 19:27:31 +13:00
Jimmy
be9f8bd0de Greasemonkey: Lift greasemonkey init app.py
To prepare for multiple-backend support.
2017-11-27 19:27:31 +13:00
Jimmy
25f626a436 Greasemonkey: Add run-at document-idle.
Supposed to be after all the assets have finished loading and in page js
has run. Not that we can garuntee that last bit. If a script misbehaves
because a precondition isn't yet met I suggest adding a defer method to
the script that adds a timer until the precondition is met.

Also changed the map/filter calls to use list comprehensions to keep
pylint happy. Even if it does look uglier.
2017-11-27 19:27:31 +13:00
Jimmy
13728387d7 Greasemonkey: Fix crash on undefined metadata. 2017-11-27 19:27:31 +13:00
Jimmy
ecdde7663f Add greasemonkey-reload command.
Also add a signal to emit when scripts are reloaded. Had to make
GreasemonkeyManager inherit from QObject to get signals to work.
2017-11-27 19:27:31 +13:00
Andor Uhlár
568d60753e Add greasemonkey compatible userscript module.
WebKit backend only for now. Loads all .js files from a directory,
specified in the greasemonkey-directory key in the storage section,
defaulting to data/greasemonkey, and wraps them in a minimal environment
providing some GM_* functions. Makes those scripts available via the
"greasemonkey" registered object in objreg and injects scripts at appropriate
times in a page load base on @run-at directives.
2017-11-27 19:27:31 +13:00
Florian Bruhin
75a8938e83 Add flake8-bugbear 2017-11-26 21:50:12 +01:00
Florian Bruhin
248a12a8b9 Add flake8-comprehensions 2017-11-26 21:42:50 +01:00
Florian Bruhin
1981239478 Add flake8-builtins 2017-11-26 21:22:33 +01:00
Florian Bruhin
5490f70b25 Remove flake8-pep3101
It seems to trigger when using modulo on ints as well, which is kind of
annoying.

See #3320
2017-11-26 20:30:42 +01:00
Florian Bruhin
b5dd647678 Upgrade pydocstyle/flake8-docstrings
See #3320
2017-11-26 20:30:32 +01:00
Florian Bruhin
7520a365eb Update comments 2017-11-26 19:43:49 +01:00
Florian Bruhin
0f3cff60fa Merge remote-tracking branch 'origin/pr/3335' 2017-11-26 19:42:54 +01:00
Florian Bruhin
95f34d755f Update changelog 2017-11-26 19:35:58 +01:00
Florian Bruhin
38f8cacd2b Merge remote-tracking branch 'origin/pr/3336' 2017-11-26 19:35:26 +01:00
Florian Bruhin
4c1f6158bd Update changelog
[ci skip]
2017-11-26 17:32:29 +01:00
Florian Bruhin
67253726fa Handle empty messages in qt_message_handler
I can't reproduce this, but someone on KDE reported always getting a crash (as
msg.splitlines()[0] gives an IndexError) when trying to select a file with
Qt 5.9.3.
2017-11-26 17:31:07 +01:00
Ryan Roden-Corrent
54fffc8264 Resolve crash when editor tab is closed.
If an editor is open on a form in a tab and that tab is closed, rewire
the callback to print a warning. Previously, the callback would access a
deleted C++ object and cause a crash.
Resolves #2758.
2017-11-26 07:50:49 -05:00
Panagiotis Ktistakis
2bb8d404d2 Adjust :bind completion tests 2017-11-26 14:07:41 +02:00
Panagiotis Ktistakis
e05dabefdf Show default keybinding in :bind completion 2017-11-26 13:34:18 +02:00
Ryan Farley
650b0051e6 remove useless ignores 2017-11-26 01:59:21 -06:00
Ryan Farley
5ed8019115 update flake8 and flake8-deprecated
Updated requirements and adjusted the configuration in `.flake8`; other
files have been modified where the lack of per-file auto-ignore caused
problems, where putty's `# flake8: disable=` syntax could be replaced
with a simpler `noqa`, or where pylint directives already suppressed the
same error.
2017-11-26 00:16:14 -06:00
狼耳
e8db59a9ef
Use socat exit status to determine if the socket is usable
Instead of checking, if *any* qutebrowser process is running (which may or may not have an IPC socket where we expect it), simply launch a new instance  *if socat fails*. 
Which it does, if: 
* the socket file doesn't exist (qutebrowser simply not running), or
* the socket isn't connectable (qutebrowser crashed, left orphaned socket)
 
Also put new instances into background, so the script behaves a bit more consistently. (Else it *sometimes* blocks and *sometimes doesn't*, when run.)
2017-11-25 11:09:57 +01:00
Jay Kamat
c9af36909f
Add tests for hint scattering 2017-11-24 13:21:21 -05:00