Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #29158 > unrolled thread

Catching http request with JavaScript

Started bymireero <mireero@free.fr>
First post2016-01-07 10:36 +0100
Last post2016-01-08 13:08 +0700
Articles 4 — 4 participants

Back to article view | Back to comp.lang.javascript


Contents

  Catching http request with JavaScript mireero <mireero@free.fr> - 2016-01-07 10:36 +0100
    Re: Catching http request with JavaScript Tim Streater <timstreater@greenbee.net> - 2016-01-07 09:44 +0000
    Re: Catching http request with JavaScript Stefan Weiss <krewecherl@gmail.com> - 2016-01-07 13:49 +0100
    Re: Catching http request with JavaScript JJ <jj4public@vfemail.net> - 2016-01-08 13:08 +0700

#29158 — Catching http request with JavaScript

Frommireero <mireero@free.fr>
Date2016-01-07 10:36 +0100
SubjectCatching http request with JavaScript
Message-ID<568e31ca$0$19736$426a74cc@news.free.fr>
Hi,

Some addons like adblocks, ghostery... are using some browser specific 
functionalities to "regular expression block" some specific http requests.

Is there any cross-browser way of achieving something similar using 
regular JavaScript?

For example I'd like to block some <img> tag or to modify on the fly 
involved request headers.

I know you cannot act on the DOM before it is loaded and after it looks 
too late but I'm still asking as my knowledge isn't 100% wide.

Thanks in advance.

[toc] | [next] | [standalone]


#29159

FromTim Streater <timstreater@greenbee.net>
Date2016-01-07 09:44 +0000
Message-ID<070120160944222492%timstreater@greenbee.net>
In reply to#29158
In article <568e31ca$0$19736$426a74cc@news.free.fr>, mireero
<mireero@free.fr> wrote:

>Hi,
>
>Some addons like adblocks, ghostery... are using some browser specific 
>functionalities to "regular expression block" some specific http requests.
>
>Is there any cross-browser way of achieving something similar using 
>regular JavaScript?
>
>For example I'd like to block some <img> tag or to modify on the fly 
>involved request headers.
>
>I know you cannot act on the DOM before it is loaded and after it looks 
>too late but I'm still asking as my knowledge isn't 100% wide.

What I do is create a new DOM element, load the html into that, then
modify *that* DOM to remove <img> or other tags I don't trust, and then
load that modified DOM into an <iframe> for display.

This also prevents the browser from doing speculative downloading of
(e.g.) images while your javascript is still running. What I had done
originally was load the HTML into the live DOM, and then modify it, on
the basis that I could do that before rendering started because
javascript is supposed to be single-threaded. But I still saw network
activity of images being downloaded and then guessed that the browser
was doing stuff behind my back.

-- 
"If you're not able to ask questions and deal with the answers without feeling 
that someone has called your intelligence or competence into question, don't 
ask questions on Usenet where the answers won't be carefully tailored to avoid
tripping your hair-trigger insecurities."                  - D M Procida, UCSM

[toc] | [prev] | [next] | [standalone]


#29161

FromStefan Weiss <krewecherl@gmail.com>
Date2016-01-07 13:49 +0100
Message-ID<n6lmt5$vb9$1@news.albasani.net>
In reply to#29158
On 01/07/2016 10:36, mireero wrote:
> Some addons like adblocks, ghostery... are using some browser specific
> functionalities to "regular expression block" some specific http requests.
> 
> Is there any cross-browser way of achieving something similar using
> regular JavaScript?

Not directly; the API required for that is not available in regular
documents. But you can always write a simple extension yourself, or add
custom user scripts with GreaseMonkey (on Firefox) or TamperMonkey (on
Chrome).

> For example I'd like to block some <img> tag or to modify on the fly
> involved request headers.

If you know the source location, you can just block an image with an ad
blocker. Other than that (depending on your requirements), a local
filtering proxy may be useful. Some desktop firewalls also have the
ability to filter/modify requests.

- stefan

[toc] | [prev] | [next] | [standalone]


#29176

FromJJ <jj4public@vfemail.net>
Date2016-01-08 13:08 +0700
Message-ID<o28thxqazlfp.yaebb4og3f7v$.dlg@40tude.net>
In reply to#29158
On Thu, 7 Jan 2016 10:36:10 +0100, mireero wrote:
> Hi,
> 
> Some addons like adblocks, ghostery... are using some browser specific 
> functionalities to "regular expression block" some specific http requests.
> 
> Is there any cross-browser way of achieving something similar using 
> regular JavaScript?
> 
> For example I'd like to block some <img> tag or to modify on the fly 
> involved request headers.
> 
> I know you cannot act on the DOM before it is loaded and after it looks 
> too late but I'm still asking as my knowledge isn't 100% wide.
> 
> Thanks in advance.

Yes by using event listeners for events such as "click" and "submit",
checking for any "source" property of elements when they're about to be
added into the document, as well as hooking object methods (e.g.
XMLHTTPRequest.send(), Document.appendChild(), Document.write(), etc.). But
these don't cover everything.

For examples: any server-side created HTML elements on web pages that use
and has an assigned external source (e.g. IFRAME, STYLE, IMG, SCRIPT,
OBJECT, etc.); and any URL that was opened from outside of any opened
document (e.g. address bar, bookmark, applications other than the current
web browser).

Most of these are beyond reach without browser specific API whether the
script is a script in the document or as a GreaseMonkey/UserScript. The last
example (applications other than the current web browser; that opens an URL)
is beyond the scope of a web browser. Although it's possible that the web
browser application be added with that feature, this one is more like a
system rather than web browser feature. Moreover, it will require
programming language other than JavaScript. e.g. C++

Monitoring/intercepting HTTP request/response headers will require browser
specific API, assuming they expose the HTTP headers rather than just the
URL.

As an alternative, you might want to use a Node.js based local HTTP/S proxy
server instead. Node.js is like the Java Runtime Environment except it uses
JavaScript instead of Java. It'll run as a separate application and be cross
browser as well as cross platform (actually it doesn't matter what the
HTTP/S client is). Moreover, you can filter web contents before they arrive
into the web browsers.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web