|
|
|
@ -5,83 +5,90 @@
|
|
|
|
|
/* eslint-env browser */
|
|
|
|
|
|
|
|
|
|
const port = browser.runtime.connect({
|
|
|
|
|
name: 'content.js',
|
|
|
|
|
});
|
|
|
|
|
name: 'content.js'
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
;(async function() {
|
|
|
|
|
if (typeof browser !== 'undefined' && typeof document.body !== 'undefined') {
|
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1000))
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
port.postMessage({ id: 'init' });
|
|
|
|
|
port.postMessage({ id: 'init' })
|
|
|
|
|
|
|
|
|
|
// HTML
|
|
|
|
|
let html = new XMLSerializer().serializeToString(document);
|
|
|
|
|
let html = new XMLSerializer().serializeToString(document)
|
|
|
|
|
|
|
|
|
|
const chunks = [];
|
|
|
|
|
const maxCols = 2000;
|
|
|
|
|
const maxRows = 3000;
|
|
|
|
|
const rows = html.length / maxCols;
|
|
|
|
|
const chunks = []
|
|
|
|
|
const maxCols = 2000
|
|
|
|
|
const maxRows = 3000
|
|
|
|
|
const rows = html.length / maxCols
|
|
|
|
|
|
|
|
|
|
let i;
|
|
|
|
|
let i
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < rows; i += 1) {
|
|
|
|
|
if (i < maxRows / 2 || i > rows - maxRows / 2) {
|
|
|
|
|
chunks.push(html.slice(i * maxCols, (i + 1) * maxCols));
|
|
|
|
|
chunks.push(html.slice(i * maxCols, (i + 1) * maxCols))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
html = chunks.join('\n');
|
|
|
|
|
html = chunks.join('\n')
|
|
|
|
|
|
|
|
|
|
// Scripts
|
|
|
|
|
const scripts = Array.prototype.slice
|
|
|
|
|
.apply(document.scripts)
|
|
|
|
|
.filter(script => script.src)
|
|
|
|
|
.map(script => script.src)
|
|
|
|
|
.filter(script => script.indexOf('data:text/javascript;') !== 0);
|
|
|
|
|
.filter((script) => script.src)
|
|
|
|
|
.map((script) => script.src)
|
|
|
|
|
.filter((script) => script.indexOf('data:text/javascript;') !== 0)
|
|
|
|
|
|
|
|
|
|
port.postMessage({ id: 'analyze', subject: { html, scripts } });
|
|
|
|
|
port.postMessage({ id: 'analyze', subject: { html, scripts } })
|
|
|
|
|
|
|
|
|
|
// JavaScript variables
|
|
|
|
|
const script = document.createElement('script');
|
|
|
|
|
const script = document.createElement('script')
|
|
|
|
|
|
|
|
|
|
script.onload = () => {
|
|
|
|
|
const onMessage = (event) => {
|
|
|
|
|
if (event.data.id !== 'js') {
|
|
|
|
|
return;
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.removeEventListener('message', onMessage);
|
|
|
|
|
window.removeEventListener('message', onMessage)
|
|
|
|
|
|
|
|
|
|
port.postMessage({ id: 'analyze', subject: { js: event.data.js } });
|
|
|
|
|
port.postMessage({ id: 'analyze', subject: { js: event.data.js } })
|
|
|
|
|
|
|
|
|
|
script.remove();
|
|
|
|
|
};
|
|
|
|
|
script.remove()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
window.addEventListener('message', onMessage);
|
|
|
|
|
window.addEventListener('message', onMessage)
|
|
|
|
|
|
|
|
|
|
port.postMessage({ id: 'get_js_patterns' });
|
|
|
|
|
};
|
|
|
|
|
port.postMessage({ id: 'get_js_patterns' })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
script.setAttribute('src', browser.extension.getURL('js/inject.js'));
|
|
|
|
|
script.setAttribute('src', browser.extension.getURL('js/inject.js'))
|
|
|
|
|
|
|
|
|
|
document.body.appendChild(script);
|
|
|
|
|
document.body.appendChild(script)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
port.postMessage({ id: 'log', subject: error });
|
|
|
|
|
port.postMessage({ id: 'log', subject: error })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})()
|
|
|
|
|
|
|
|
|
|
port.onMessage.addListener((message) => {
|
|
|
|
|
switch (message.id) {
|
|
|
|
|
case 'get_js_patterns':
|
|
|
|
|
postMessage({
|
|
|
|
|
postMessage(
|
|
|
|
|
{
|
|
|
|
|
id: 'patterns',
|
|
|
|
|
patterns: message.response.patterns,
|
|
|
|
|
}, window.location.href);
|
|
|
|
|
patterns: message.response.patterns
|
|
|
|
|
},
|
|
|
|
|
window.location.href
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
break
|
|
|
|
|
default:
|
|
|
|
|
// Do nothing
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// https://stackoverflow.com/a/44774834
|
|
|
|
|
// https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/executeScript#Return_value
|
|
|
|
|
undefined; // eslint-disable-line no-unused-expressions
|
|
|
|
|
undefined // eslint-disable-line no-unused-expressions
|
|
|
|
|