Как изменить информацию об отпечатке пальца холста, WebGL и аудио с помощью фреймворка pyppeter, чтобы его можно было изменять в виде плагинов? Необходимо установить значение headless = false
, но плагин не может работать в автономном режиме.
Код, который я извлек из Chrome плагина, не вступил в силу после выполнения.
Используйте надстройки браузера:
- Отпечаток AudioContext
- Отпечаток холста
- Отпечаток WebGL
import asyncio
from pyppeteer import launch
from fake_useragent import UserAgent
import time
width, height = 1366, 768
async def main():
# 更换头部
user_agent = UserAgent()
browser = await launch(headless=False)
page = await browser.newPage()
await page.setViewport({'width': width, 'height': height})
await page.setUserAgent(user_agent.random)
await page.evaluateOnNewDocument('''() => {
const toBlob = HTMLCanvasElement.prototype.toBlob;
const toDataURL = HTMLCanvasElement.prototype.toDataURL;
const getImageData = CanvasRenderingContext2D.prototype.getImageData;
//
var noisify = function (canvas, context) {
const shift = {
'r': Math.floor(Math.random() * 10) - 5,
'g': Math.floor(Math.random() * 10) - 5,
'b': Math.floor(Math.random() * 10) - 5,
'a': Math.floor(Math.random() * 10) - 5
};
//
const width = canvas.width, height = canvas.height;
const imageData = getImageData.apply(context, [0, 0, width, height]);
for (let i = 0; i < height; i++) {
for (let j = 0; j < width; j++) {
const n = ((i * (width * 4)) + (j * 4));
imageData.data[n + 0] = imageData.data[n + 0] + shift.r;
imageData.data[n + 1] = imageData.data[n + 1] + shift.g;
imageData.data[n + 2] = imageData.data[n + 2] + shift.b;
imageData.data[n + 3] = imageData.data[n + 3] + shift.a;
}
}
//
context.putImageData(imageData, 0, 0);
};
//
Object.defineProperty(HTMLCanvasElement.prototype, "toBlob", {
"value": function () {
noisify(this, this.getContext("2d"));
return toBlob.apply(this, arguments);
}
});
//
Object.defineProperty(HTMLCanvasElement.prototype, "toDataURL", {
"value": function () {
noisify(this, this.getContext("2d"));
return toDataURL.apply(this, arguments);
}
});
//
Object.defineProperty(CanvasRenderingContext2D.prototype, "getImageData", {
"value": function () {
noisify(this.canvas, this);
return getImageData.apply(this, arguments);
}
});
//
document.documentElement.dataset.cbscriptallow = true;
}''')
await page.goto('https://browserleaks.com/canvas')
#await page.goto('http://www.sdfymj.com/ua.php')
await asyncio.sleep(3)
time.sleep(600)
await browser.close()
asyncio.get_event_loop().run_until_complete(main())