Commit Graph

14113 Commits

Author SHA1 Message Date
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
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
Florian Bruhin
03a9cbdfb4 Break long lines
(cherry picked from commit 18a45bbd5b0492785cfc6e9b541d816ed8ec5c54)
2017-11-24 16:03:34 +01:00
Florian Bruhin
06fc52321e Ignore new Qt 5.9.3 error message 2017-11-24 14:24:15 +01:00
Florian Bruhin
15c7ede916 Update to PyQt 5.9.2 2017-11-24 14:24:15 +01:00
Florian Bruhin
0afd6b23c9 Add the option name to the backend error message 2017-11-24 09:25:45 +01:00
Florian Bruhin
28d7c5e204 Fix lint 2017-11-24 07:25:10 +01:00
Ryan Roden-Corrent
8eab402820 Abort :edit-command on invalid input.
Show an error message if the user edits the command such that it is
missing a start character (:, /, or ?). Previously, this would cause the
browser to crash.

Resolves #3326.
2017-11-23 08:15:27 -05:00
Florian Bruhin
b31360b6e3 Fix line break 2017-11-22 17:19:21 +01:00
Florian Bruhin
750d2c490f Fix :completion-item-focus --history with / or ?
If we have no completion (like when searching), we can just always go through
history with up/down.
2017-11-22 15:34:48 +01:00
Florian Bruhin
5d8c9577a7 Improve hypothesis example generation for configtypes.Dict 2017-11-22 11:53:49 +01:00
Florian Bruhin
72d466d236 Break long lines 2017-11-22 11:34:10 +01:00
Florian Bruhin
487951cd31 Fix joining :jseval path 2017-11-22 10:57:48 +01:00
Florian Bruhin
cbf5fc01fa Avoid more about:blank loads 2017-11-22 10:15:18 +01:00
Florian Bruhin
221b81ae90 Fix typo in changelog 2017-11-22 09:37:17 +01:00
Florian Bruhin
12f4940ef3 Make :jseval use a fixed path with relative paths 2017-11-22 09:37:17 +01:00