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.