-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcustom-selectors.js
47 lines (41 loc) · 1.57 KB
/
custom-selectors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*
HTCRAWL
https://github.com/fcavallarin/htcrawl
Author: [email protected]
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
*/
"use strict";
const puppeteer = require('puppeteer');
puppeteer.Puppeteer.registerCustomQueryHandler('inframe', {
queryOne: async (element, selector) => {
// Should be safe to use ' ; ' as separator. Ofc data-x='a ; b' can brake it
const tokens = selector.split(" ; ");
let frames = element.querySelectorAll(tokens[0]);
for(let i = 1; i < tokens.length; i++){
// TODO discend all frames
try{
frames = frames[0].contentWindow.document.querySelectorAll(tokens[i]);
}catch(e){
throw `Element '${tokens[i]}' not found, not a Frame or not on the same origin`;
}
}
return frames[0] || null;
},
queryAll: async (element, selector) => {
// Should be safe to use ' ; ' as separator. Ofc data-x='a ; b' can brake it
const tokens = selector.split(" ; ");
let frames = element.querySelectorAll(tokens[0]);
for(let i = 1; i < tokens.length; i++){
// TODO discend all frames
try{
frames = frames[0].contentWindow.document.querySelectorAll(tokens[i]);
}catch(e){
throw `Element '${tokens[i]}' not found, not a Frame or not on the same origin`;
}
}
return frames;
}
});