DOM module reference - adsbypasser/adsbypasser GitHub Wiki
This module provides DOM related feature, almost all AdsBypasser needs are in this module.
-
Functions
- AJAX
- Cookie
- DOM
- Redirecting
- Utility
Although I call $
is a module, in fact it is a function.
This function will returns the first element within the context
(using depth-first pre-order traversal of the document's nodes) that matches the specified group of selector
(a string containing CSS3 selectors).
If selector
can not found in context
, will throws DomNotFoundError
exception.
Example:
<!-- ... -->
<div id="dom-id">
<!-- ... -->
</div>
<!-- ... -->
let i = $('#dom-id');
// i is the div element
Same as $()
, but returns null
instead of throw exception if not found.
Example:
let i = $.$('img');
if (!i) {
/* other action */;
}
Same as $()
, but returns all matched elements. If no element matches selector
, an empty NodeList is returned.
Example:
let s = $.$$('script');
s = _.find(s, (script) => {
return script.textContent.indexOf('redirect');
});
// s is the first matched script tag
Removes all nodes within the context
that matches the specified group of selector
.
Example:
$.remove('iframe');
// removes all iframe
Converts the rawHTML
text to an XMLDocument object that can be used with standards Javascript functions and AdsBypasser DOM functions as a context.
Example:
let doc = '<html><body><a id="redirect" href="http://example.org">Redirect</a></body></html>';
// Convert our raw HTML to an XMLDocument for easier parsing
let XMLDoc = $.toDOM(doc);
let redirectURL = $('#redirect', XMLDoc);
await $.openLink(redirectURL);
Finds the first script in the context
(by default document
) which is matched by pattern
and return the matches.
pattern
can be a RegExp
or a string. If pattern
is a RegExp
, it will return the matched groups. If pattern
is a string, it will return whole script content.
If pattern
is not a RegExp
nor a string, or given pattern
not found, returns null
.
Example:
let doc = [
'<html>',
'<head>',
'<script>document.location.href="http://example.org";</script>',
'<script>var ads = "/* some complicated code */&url=example.org"</script>',
'</head>',
'</html>',
];
// Create a fake page for the example
let XMLDoc = $.toDOM(doc);
// Match in the scripts of the page
let matches = $.searchFromScripts('var ads', XMLDoc);
if (!matches.match(/url=([^&]+)/)) {
return;
}
matches = $.searchFromScripts(/document\.location\.href="([^"]+)";/, XMLDoc);
let redirectURL = matches[1];
await $.openLink(redirectURL);
Redirect to url
.
options
have the following keys:
referer
If it is true
, HTTP Referer
header will be sent. Defaults to true.
post
An object or a string that contains POST data. If this key is set, the request will use POST, otherwise use GET. Defaults to undefined.
Examples:
let redirecter = $('a');
await $.openLink(redirecter.href);
// will open a link that will redirect to the website, it checks the referer so we need to use this function
let a = $('a');
await $.openLink(a.href, {
referer: false,
});
// will redirect to the link without Referer
let f = $('form');
await $.openLink(f.action, {
post: {},
});
// POST to form target
Redirect to the image imageUrl
.
options
have the following keys:
replace
Stays on current page, and replace the whole page content with a image which imageUrl
points to.
This is useful when browsers can not show the image directly (e.g.: forcing download attachment, wrong mime type ... etc.).
Defaults to false
referer
If it is true
, HTTP Referer
header will be sent. Defaults to false.
Example:
let img = $('img');
await $.openImage(img.src);
// will redirect to the image
Make an AJAX request to url
by POST
method. data
can be a query string or an object. headers
is an object or an array-like object that will add and overwrite default headers.
If data
is an object, it will be recursively serialize to a query string.
Returns a Promise, which has an extra method: abort()
, used for canceling the request.
Example:
let text = await $.post('/ajax', {
args: {
lid: 123,
oid: 456,
},
opt: 'make_log',
});
// POST /ajax?args%5Blid%5D=123&args%5Boid%5D=456&opt=make_log
Make an AJAX request to url
(relative or absolute url) by GET
method. data
can be a query string or an object. headers
is an object or an array-like object that will add and overwrite default headers.
If data
is an object, it will be recursively serialize to a query string.
Returns a Promise, which has an extra method: abort()
, used for canceling the request.
Example:
let text = await $.get('/frame', {
frameinfo: {
id: 123,
number: 456,
},
});
// GET /frame?frameinfo%5Bid%5D=123&frameinfo%5Bnumber%5D=456
Returns the content of the cookie of the current document with name key
.
Sets or replace the content of the cookie of the current document with name key
by the value value
.
Example:
$.setCookie('user', 'true');
// Set the current page cookie named 'user' to the value 'true'
Removes (expires) all cookies.
Removes all timer registered by window.setInterval
.
Starting from Firefox 30+, the unsafeWindow
uses new policy, see here.
In short, page-script can not normally access Object
, Array
, Function
from userscript anymore. (but primitive types are okay, e.g.: undefined
, null
, String
, Number
... etc)
You must use cloneInto
and exportFunction
to convert objects from sandbox.
$.window
is an adapter to deal with this.
You don't need to care about which object should be converted, just remember to use $.window
instead of unsafeWindow
.
Example:
// page-script can not call wrongFunction
unsafeWindow.wrongFunction = function () {/* ... */};
// page-script can call rightFunction
$.window.rightFunction = function () {/* ... */};
// page-script can not access property in Array or Object
unsafeWindow.aFunction([1,2,3], {a: 'b'});
// you should use $.window
$.window.aFunction([1,2,3], {a: 'b'});