librelist archives

« back to archive

Control conkeror in Emacs with mozrepl

Control conkeror in Emacs with mozrepl

From:
zwz
Date:
2014-10-11 @ 11:41
I have been carrying this idea for a long time. After I came across this
little tool(https://github.com/RenWenshan/emacs-moz-controller), I decide
to give it a shot.

The first thing I want to implement is to make conkeror create a new
buffer and open some url in Emacs, without bringing conkeror to the
front of my desktop.

I have figured out that I can make conkeror open some url within current
buffer by

conkeror.get_recent_conkeror_window().buffers.current.document.location.href="<some
url>" in mozrepl.

But I have no idea how to open a new buffer first. Any hints?

Re: [conkeror] Control conkeror in Emacs with mozrepl

From:
Scott Jaderholm
Date:
2014-10-12 @ 22:09
For people who don't mind the conkeror window gaining focus, they can use this:

(shell-command "conkeror -e \"load_url_in_new_buffer('http://reddit.com')\"")
(shell-command "conkeror -e
\"load_url_in_current_buffer('http://google.com')\"")

If you look at load_url_in_new_buffer, you'll see both how to create a
buffer (buffer_creator):

function load_url_in_new_buffer (url, ctx) {
    create_buffer_in_current_window(
        buffer_creator(content_buffer,
                       $opener = ctx,
                       $load = url),
        OPEN_NEW_BUFFER, true /* focus the new window */);
}

I thought passing false at the end would prevent the window from
raising, but it does not, and I'm not sure how to do that or if it is
possible outside of using you window manager to deny the raise.
Scott


On Sat, Oct 11, 2014 at 4:41 AM, zwz <zhangweize@gmail.com> wrote:
>
> I have been carrying this idea for a long time. After I came across this
> little tool(https://github.com/RenWenshan/emacs-moz-controller), I decide
> to give it a shot.
>
> The first thing I want to implement is to make conkeror create a new
> buffer and open some url in Emacs, without bringing conkeror to the
> front of my desktop.
>
> I have figured out that I can make conkeror open some url within current
> buffer by
> 
conkeror.get_recent_conkeror_window().buffers.current.document.location.href="<some
> url>" in mozrepl.
>
> But I have no idea how to open a new buffer first. Any hints?
>

Re: [conkeror] Control conkeror in Emacs with mozrepl

From:
zwz
Date:
2014-10-13 @ 01:33
Thank you for your tips.
The true/false at the end is to focus on the new buffer or not.

I tried in mozrepl with conkeror.create_buffer_in_current_window(...)
but it raises conkeror.

Seems there is no option to make conkeror stay behind when creating a
new buffer.


Scott Jaderholm <jaderholm@gmail.com> writes:

> For people who don't mind the conkeror window gaining focus, they can use this:
>
> (shell-command "conkeror -e \"load_url_in_new_buffer('http://reddit.com')\"")
> (shell-command "conkeror -e
> \"load_url_in_current_buffer('http://google.com')\"")
>
> If you look at load_url_in_new_buffer, you'll see both how to create a
> buffer (buffer_creator):
>
> function load_url_in_new_buffer (url, ctx) {
>     create_buffer_in_current_window(
>         buffer_creator(content_buffer,
>                        $opener = ctx,
>                        $load = url),
>         OPEN_NEW_BUFFER, true /* focus the new window */);
> }
>
> I thought passing false at the end would prevent the window from
> raising, but it does not, and I'm not sure how to do that or if it is
> possible outside of using you window manager to deny the raise.
> Scott
>
>
> On Sat, Oct 11, 2014 at 4:41 AM, zwz <zhangweize@gmail.com> wrote:
>>
>> I have been carrying this idea for a long time. After I came across this
>> little tool(https://github.com/RenWenshan/emacs-moz-controller), I decide
>> to give it a shot.
>>
>> The first thing I want to implement is to make conkeror create a new
>> buffer and open some url in Emacs, without bringing conkeror to the
>> front of my desktop.
>>
>> I have figured out that I can make conkeror open some url within current
>> buffer by
>> 
conkeror.get_recent_conkeror_window().buffers.current.document.location.href="<some
>> url>" in mozrepl.
>>
>> But I have no idea how to open a new buffer first. Any hints?
>>

Re: [conkeror] Control conkeror in Emacs with mozrepl

From:
zwz
Date:
2014-10-13 @ 01:36
I also tried 
var ctx={};
ctx.__proto__ = conkeror;
    ctx.conkeror = conkeror;
    ctx.window = conkeror.get_recent_conkeror_window();
    ctx.buffer = ctx.window.buffers.current;
    ctx.document = ctx.buffer.document;
conkeror.buffer_creator(content_buffer,$opener = ctx,$load = "...")

It runs without any error, not raising conkeror, but no buffer either.


Scott Jaderholm <jaderholm@gmail.com> writes:

> For people who don't mind the conkeror window gaining focus, they can use this:
>
> (shell-command "conkeror -e \"load_url_in_new_buffer('http://reddit.com')\"")
> (shell-command "conkeror -e
> \"load_url_in_current_buffer('http://google.com')\"")
>
> If you look at load_url_in_new_buffer, you'll see both how to create a
> buffer (buffer_creator):
>
> function load_url_in_new_buffer (url, ctx) {
>     create_buffer_in_current_window(
>         buffer_creator(content_buffer,
>                        $opener = ctx,
>                        $load = url),
>         OPEN_NEW_BUFFER, true /* focus the new window */);
> }
>
> I thought passing false at the end would prevent the window from
> raising, but it does not, and I'm not sure how to do that or if it is
> possible outside of using you window manager to deny the raise.
> Scott
>
>
> On Sat, Oct 11, 2014 at 4:41 AM, zwz <zhangweize@gmail.com> wrote:
>>
>> I have been carrying this idea for a long time. After I came across this
>> little tool(https://github.com/RenWenshan/emacs-moz-controller), I decide
>> to give it a shot.
>>
>> The first thing I want to implement is to make conkeror create a new
>> buffer and open some url in Emacs, without bringing conkeror to the
>> front of my desktop.
>>
>> I have figured out that I can make conkeror open some url within current
>> buffer by
>> 
conkeror.get_recent_conkeror_window().buffers.current.document.location.href="<some
>> url>" in mozrepl.
>>
>> But I have no idea how to open a new buffer first. Any hints?
>>

Re: [conkeror] Control conkeror in Emacs with mozrepl

From:
Scott Jaderholm
Date:
2014-10-13 @ 03:34
If you happen to be using StumpWM, then here's a way to do it:

In .stumpwmrc:

(defcommand conkeror-deny-raise-request () ()
  (pushnew '(:class "Conkeror") stumpwm:*deny-raise-request* :test #'equal))

(defcommand conkeror-allow-raise-request () ()
  (setq stumpwm:*deny-raise-request*
        (delete '(:class "Conkeror") stumpwm:*deny-raise-request*
:test #'equal)))

In .emacs:

(defun browse-url-conkeror-background (url)
  (shell-command "stumpish conkeror-deny-raise-request")
  (shell-command (concat "conkeror -e \"load_url_in_new_buffer('" url "')\""))
  (shell-command "stumpish conkeror-allow-raise-request"))
Scott


On Sun, Oct 12, 2014 at 6:36 PM, zwz <zhangweize@gmail.com> wrote:
>
> I also tried
> var ctx={};
> ctx.__proto__ = conkeror;
>     ctx.conkeror = conkeror;
>     ctx.window = conkeror.get_recent_conkeror_window();
>     ctx.buffer = ctx.window.buffers.current;
>     ctx.document = ctx.buffer.document;
> conkeror.buffer_creator(content_buffer,$opener = ctx,$load = "...")
>
> It runs without any error, not raising conkeror, but no buffer either.
>
>
> Scott Jaderholm <jaderholm@gmail.com> writes:
>
>> For people who don't mind the conkeror window gaining focus, they can use this:
>>
>> (shell-command "conkeror -e \"load_url_in_new_buffer('http://reddit.com')\"")
>> (shell-command "conkeror -e
>> \"load_url_in_current_buffer('http://google.com')\"")
>>
>> If you look at load_url_in_new_buffer, you'll see both how to create a
>> buffer (buffer_creator):
>>
>> function load_url_in_new_buffer (url, ctx) {
>>     create_buffer_in_current_window(
>>         buffer_creator(content_buffer,
>>                        $opener = ctx,
>>                        $load = url),
>>         OPEN_NEW_BUFFER, true /* focus the new window */);
>> }
>>
>> I thought passing false at the end would prevent the window from
>> raising, but it does not, and I'm not sure how to do that or if it is
>> possible outside of using you window manager to deny the raise.
>> Scott
>>
>>
>> On Sat, Oct 11, 2014 at 4:41 AM, zwz <zhangweize@gmail.com> wrote:
>>>
>>> I have been carrying this idea for a long time. After I came across this
>>> little tool(https://github.com/RenWenshan/emacs-moz-controller), I decide
>>> to give it a shot.
>>>
>>> The first thing I want to implement is to make conkeror create a new
>>> buffer and open some url in Emacs, without bringing conkeror to the
>>> front of my desktop.
>>>
>>> I have figured out that I can make conkeror open some url within current
>>> buffer by
>>> 
conkeror.get_recent_conkeror_window().buffers.current.document.location.href="<some
>>> url>" in mozrepl.
>>>
>>> But I have no idea how to open a new buffer first. Any hints?
>>>

Re: Control conkeror in Emacs with mozrepl

From:
zwz
Date:
2014-10-13 @ 10:52
Yes, I am using stumpwm :)

It works, although I prefer a more general(not depending on stumpwm)
way.

Thank you.

Scott Jaderholm <jaderholm@gmail.com> writes:

> If you happen to be using StumpWM, then here's a way to do it:
>
> In .stumpwmrc:
>
> (defcommand conkeror-deny-raise-request () ()
>   (pushnew '(:class "Conkeror") stumpwm:*deny-raise-request* :test #'equal))
>
> (defcommand conkeror-allow-raise-request () ()
>   (setq stumpwm:*deny-raise-request*
>         (delete '(:class "Conkeror") stumpwm:*deny-raise-request*
> :test #'equal)))
>
> In .emacs:
>
> (defun browse-url-conkeror-background (url)
>   (shell-command "stumpish conkeror-deny-raise-request")
>   (shell-command (concat "conkeror -e \"load_url_in_new_buffer('" url "')\""))
>   (shell-command "stumpish conkeror-allow-raise-request"))
> Scott
>
>
> On Sun, Oct 12, 2014 at 6:36 PM, zwz <zhangweize@gmail.com> wrote:
>>
>> I also tried
>> var ctx={};
>> ctx.__proto__ = conkeror;
>>     ctx.conkeror = conkeror;
>>     ctx.window = conkeror.get_recent_conkeror_window();
>>     ctx.buffer = ctx.window.buffers.current;
>>     ctx.document = ctx.buffer.document;
>> conkeror.buffer_creator(content_buffer,$opener = ctx,$load = "...")
>>
>> It runs without any error, not raising conkeror, but no buffer either.
>>
>>
>> Scott Jaderholm <jaderholm@gmail.com> writes:
>>
>>> For people who don't mind the conkeror window gaining focus, they can 
use this:
>>>
>>> (shell-command "conkeror -e \"load_url_in_new_buffer('http://reddit.com')\"")
>>> (shell-command "conkeror -e
>>> \"load_url_in_current_buffer('http://google.com')\"")
>>>
>>> If you look at load_url_in_new_buffer, you'll see both how to create a
>>> buffer (buffer_creator):
>>>
>>> function load_url_in_new_buffer (url, ctx) {
>>>     create_buffer_in_current_window(
>>>         buffer_creator(content_buffer,
>>>                        $opener = ctx,
>>>                        $load = url),
>>>         OPEN_NEW_BUFFER, true /* focus the new window */);
>>> }
>>>
>>> I thought passing false at the end would prevent the window from
>>> raising, but it does not, and I'm not sure how to do that or if it is
>>> possible outside of using you window manager to deny the raise.
>>> Scott
>>>
>>>
>>> On Sat, Oct 11, 2014 at 4:41 AM, zwz <zhangweize@gmail.com> wrote:
>>>>
>>>> I have been carrying this idea for a long time. After I came across this
>>>> little tool(https://github.com/RenWenshan/emacs-moz-controller), I decide
>>>> to give it a shot.
>>>>
>>>> The first thing I want to implement is to make conkeror create a new
>>>> buffer and open some url in Emacs, without bringing conkeror to the
>>>> front of my desktop.
>>>>
>>>> I have figured out that I can make conkeror open some url within current
>>>> buffer by
>>>> 
conkeror.get_recent_conkeror_window().buffers.current.document.location.href="<some
>>>> url>" in mozrepl.
>>>>
>>>> But I have no idea how to open a new buffer first. Any hints?
>>>>

Automate browsing

From:
Eike Kettner
Date:
2014-10-13 @ 11:44
Hello List,

I'd like to have a command that mimics a little the macro feature from
editors as emacs. I'd like to choose a link with hinting and then have
conkeror find its way to a download url that is 2 more clicks away.  It
should open the first link in a new buffer, so it can be closed at the
end to get me back to where I started.

Is there any documentation or examples on this? I'm new to conkeror and
I only managed it to write a command that does the same as C-u f. But
then I'm stuck… I don't know how to operate further on the new buffer.

In my case I actually don't need a very sophisticated solution. If it is
possible to simulate key-strokes and play with the delay in between,
that would be fine.


Thanks and regards
Eike

automate browsing

From:
Eike Kettner
Date:
2014-10-15 @ 18:20
Hello List,

I'd like to choose a link with hinting and then have conkeror find its
way to a download url that is 2 more clicks away.  It should open the
first link in a new buffer, so it can be closed at the end to get me
back to where I started.

Is there any documentation or examples on this? I'm new to conkeror and
I only managed to write some very hacky function… I'm posting it
anyways, since it works for me. But I'm interested in tips and links
that would help me to improve it:

interactive("ugly-download",
    "my doc string",
    function(I) {
        var mb = I.window.minibuffer;
        bo = yield read_browser_object(I);
        link = load_spec_uri_string(load_spec(bo));
        (yield browser_object_follow(I.buffer, OPEN_NEW_BUFFER, link));
        I.window.setTimeout(function () {
            var len = I.window.buffers.buffer_list.length;
            var buf = I.window.buffers.buffer_list[len -1];
            var dl = find_link(buf.document, "some-href-substring");
            if (! dl) {
                mb.message("No download page found!");
            } else {
                var url = buf.current_uri.spec + dl;
                buf.document.location = url;
                I.window.setTimeout(function () {
                    var file = find_link(buf.document, "another-href-substring");
                    if (file) {
                        shell_command_blind("wget " + file, $cwd = cwd);
                        kill_buffer(buf);
                    } else {
                        mb.message("No download link found.");
                    }
                }, 1000);
            }
        }, 1000);
    },
    $browser_object = browser_object_links);

The `find_url' function just loops through the "a" elements of
`buf.document' and returns the href attribute of the first where the
given substring can be found.

Is there a way to omit `setTimeout' and be notified when the page
finished loading? And is there simpler way in general? I first tried to
call the commands behind the keys (like a macro), but I couldn't make it
work that way.

Thanks and regards
Eike

Re: [conkeror] automate browsing

From:
Sean McAfee
Date:
2014-10-15 @ 19:01
Eike Kettner <eike.kettner@posteo.de> writes:
> Hello List,
>
> I'd like to choose a link with hinting and then have conkeror find its
> way to a download url that is 2 more clicks away.  It should open the
> first link in a new buffer, so it can be closed at the end to get me
> back to where I started.
>
> Is there any documentation or examples on this? I'm new to conkeror and
> I only managed to write some very hacky function… I'm posting it
> anyways, since it works for me. But I'm interested in tips and links
> that would help me to improve it:

I poked around recently trying to do full HTTP requests from Conkeror
code, and the best I could come up with was to use XMLHttpRequest
thusly:

function my_request(url, callback) {
    const req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
        .createInstance(Ci.nsIXMLHttpRequest);
    req.open("GET", url, true);
    req.responseType = "document";
    req.onreadystatechange = function () {
        if (this.readyState == 4) {
            callback(this.response);
        }
    };
    req.send(null);
}

For example, here's a command I wrote to test that routine that fetches
the CNN home page, then follows a random hyperlink and displays the
title of the linked page:

interactive("cnn", "title of random CNN page", function (I) {
    my_request("http://cnn.com/", function (document) {
        const anchors = document.getElementsByTagName("A");
        const anchor = anchors[Math.floor(Math.random() * anchors.length)];
        my_request(anchor.href, function (document) {
            I.minibuffer.message(document.title);
        })
    });
});

Of course in general you'd need to be careful about relative URLs and
such.

Conkeror has a built-in function send_http_request() in modules/utils.js
that does something similar, but it doesn't seem to allow for setting
the responseType attribute to "document" to parse the response into a
document tree, as is needed for tasks like this.

Anyway, I'm not really familiar with writing commands that operate on
hinted links, but hopefully you can work something like the above into
your code.


--Sean

Re: [conkeror] automate browsing

From:
Eike Kettner
Date:
2014-10-15 @ 20:26
Sean McAfee writes:
> I poked around recently trying to do full HTTP requests from Conkeror
> code, and the best I could come up with was to use XMLHttpRequest
> thusly:
>
> function my_request(url, callback) {
>     const req = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
>         .createInstance(Ci.nsIXMLHttpRequest);
>     req.open("GET", url, true);
>     req.responseType = "document";
>     req.onreadystatechange = function () {
>         if (this.readyState == 4) {
>             callback(this.response);
>         }
>     };
>     req.send(null);
> }
>
> For example, here's a command I wrote to test that routine that fetches
> the CNN home page, then follows a random hyperlink and displays the
> title of the linked page:
>
> interactive("cnn", "title of random CNN page", function (I) {
>     my_request("http://cnn.com/", function (document) {
>         const anchors = document.getElementsByTagName("A");
>         const anchor = anchors[Math.floor(Math.random() * anchors.length)];
>         my_request(anchor.href, function (document) {
>             I.minibuffer.message(document.title);
>         })
>     });
> });
>
> Of course in general you'd need to be careful about relative URLs and
> such.
>
> Conkeror has a built-in function send_http_request() in modules/utils.js
> that does something similar, but it doesn't seem to allow for setting
> the responseType attribute to "document" to parse the response into a
> document tree, as is needed for tasks like this.

Ah, that looks much better! Thank you for the hints and the snippets,
I'm going to play with them.

Regards,
Eike