Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

javascript bookmarklets/quickmarks #221

Open
The-Compiler opened this issue Oct 27, 2014 · 11 comments

Comments

Projects
None yet
6 participants
@The-Compiler
Copy link
Collaborator

commented Oct 27, 2014

javascript:... quickmarks do not work and we get a prompt to open with external handler instead.

From eric's HelpBrowserWV.py:

def setSource(self, name, requestData=None):
    """
    Public method used to set the source to be displayed.

    @param name filename to be shown (QUrl)
    @param requestData tuple containing the request data (QNetworkRequest,
        QNetworkAccessManager.Operation, QByteArray)
    """
    if (name is None or not name.isValid()) and requestData is None:
        return

    if name is None and requestData is not None:
        name = requestData[0].url()

    if self.ctrlPressed:
        # open in a new window
        self.mw.newTab(name)
        self.ctrlPressed = False
        return

    if not name.scheme():
        name.setUrl(Preferences.getHelp("DefaultScheme") + name.toString())

    # [...]

    elif name.scheme() == "javascript":
        scriptSource = QUrl.fromPercentEncoding(name.toString(
            QUrl.FormattingOptions(QUrl.TolerantMode | QUrl.RemoveScheme)))
        self.page().mainFrame().evaluateJavaScript(scriptSource)
        return

    # [...]

    if requestData is not None:
        self.load(*requestData)
    else:
        self.load(name)

@The-Compiler The-Compiler self-assigned this Oct 27, 2014

@The-Compiler The-Compiler added this to the v0.1 milestone Oct 27, 2014

@The-Compiler

This comment has been minimized.

Copy link
Collaborator Author

commented Oct 27, 2014

This belongs in widgets/webview.py:WebView:openurl but I'm unsure how to interpret the javascript URLs correctly.

Related standard: https://html.spec.whatwg.org/multipage/browsers.html#navigating-across-documents step 15

@The-Compiler

This comment has been minimized.

Copy link
Collaborator Author

commented Oct 27, 2014

22:32 <zcorpan> The-Compiler: just avoid letting users run javascript: from the address bar since that's a social engineering attack :-)

This is a good point, especially with pp etc. It probably only should work for quick-/bookmarks.

@The-Compiler The-Compiler removed this from the v0.1 milestone Nov 12, 2014

@The-Compiler The-Compiler removed their assignment Oct 1, 2015

@Anaphory

This comment has been minimized.

Copy link

commented Jan 9, 2017

I thought I had got this to work some time earlier, so I am surprised it didn't work and this bug exists. Read the following just as a possible use case for JS in *marks, which could also be served by some other method of executing a specific constant JS on an arbitrary webpage bindable to a key binding.

I have created a quickmark for “Save to Zotero library” with the Javascript link javascript:var%20d=document,s=d.createElement("script");s.src="https://www.zotero.org/bookmarklet/loader.js";(d.body?d.body:d.documentElement).appendChild(s);void(0);. The %20 is there because of #2225, but instead of some reasonable or garbled code being exectued, Qute asks me “Open external application for javascript link?”

@The-Compiler

This comment has been minimized.

Copy link
Collaborator Author

commented Jan 9, 2017

which could also be served by some other method of executing a specific constant JS on an arbitrary webpage bindable to a key binding.

You can already bind a key to a :jseval command: :bind ,h jseval alert("Hello World")

@edrex

This comment has been minimized.

Copy link
Contributor

commented Mar 5, 2017

I tried adding a bookmarklet with 0.10/OSX (webengine) but it fails to evaluate.

The bookmarklet (Source: https://www.comic-rocket.com/help/bookmarklets/):

javascript:location.href='https://www.comic-rocket.com/go?mark&nav=next&uri='+encodeURIComponent(location.href)

Stderr:

14:43:08 ERROR: [:1] Uncaught SyntaxError: Unexpected token :
@The-Compiler

This comment has been minimized.

Copy link
Collaborator Author

commented Mar 6, 2017

@edrex That looks like you could simply bind (or add an alias for) :open https://www.comic-rocket.com/go?mark&nav=next&uri={url} FWIW.

@cincodenada

This comment has been minimized.

Copy link
Contributor

commented May 5, 2018

I came here looking for a way to use Instapaper's bookmarklet, and jseval is a good stopgap. I would love to have it supported in quickmarks/bookmarks as well, it makes more sense to keep it there for me than to set up a binding for every bookmarklet I might want to run, but for now, I did a quick:

:bind ,insta jseval <bookmarklet blob>

And now ,insta saves things to my Instapaper like I wanted. Thanks!

@olmokramer

This comment has been minimized.

Copy link
Contributor

commented May 5, 2018

@cincodenada You can also use aliases for that, which IMO makes even more sense than making a keybinding. In your config.py add

c.aliases.update({ 'insta': 'jseval <bookmarklet blob>' })

Now you can do :insta from qutebrowser's command line, and you also don't take up valuable keybinding space 😉.

@cincodenada

This comment has been minimized.

Copy link
Contributor

commented May 6, 2018

Oh neat, thanks for the tip! Even better 😃

@toofar

This comment has been minimized.

Copy link
Collaborator

commented Oct 30, 2018

With QtWebEngine jseval ... and open javascript:... seem to have different behaviour in at least one case. Someone posted this in IRC the other day:

:jseval location.href='org-protocol://capture?url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)

Leads to chrome-error://chromewebdata/ whereas

:open javascript:location.href='org-protocol://capture?url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)

Leads to org-protocol... being passed to xdg-open, or something that opens another application.

Running :quickmark-add javascript:location.href='org-protocol://capture?url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title) orgmode does indeed lead to a Uncaught SyntaxError: Unexpected token : error when opening the quickmark but that is just a quoting intricacy. Running that with another layer of quoting (:quickmark-add "javascript:location.href='org-protocol://capture?url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)" orgmode) causes opening that quickmark to cause an external application to be opened.

@The-Compiler

This comment has been minimized.

Copy link
Collaborator Author

commented Oct 30, 2018

With QtWebEngine jseval ... and open javascript:... seem to have different behaviour in at least one case.

I think that's related to Chromium's detection whether something resulted from an user action. It only calls xdg-open when it thinks the user actually clicked on a link (rather than the page opening it automatically via JS). I suppose :jseval has the same result like a page executing that JS (i.e., an automated action), while opening a javascript: link via :open is akin to the user entering it in Chromium's address bar (i.e., a user-requested action).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.