mirror of
https://github.com/DIYgod/RSSHub.git
synced 2025-12-02 18:18:06 +08:00
feat(utils): random user agent (#9449)
* feat(utils): random ua * chore: bump rand-user-agent to 1.0.58(no more deps)
This commit is contained in:
29
lib/utils/rand-user-agent.js
Normal file
29
lib/utils/rand-user-agent.js
Normal file
@@ -0,0 +1,29 @@
|
||||
const randUserAgent = require('rand-user-agent');
|
||||
|
||||
/**
|
||||
* A handy function to help generate a legit useragent.
|
||||
*
|
||||
* @param {Object} randUserAgent
|
||||
* @param {string} randUserAgent.browser Name of a browser, case-insensitive. `chrome`, `edge`, `firefox`, `mobile safari`(ios only) or `safari`.
|
||||
* @param {string} randUserAgent.os Name of an OS, case-insensitive. `android`, `ios`, `mac os`, `linux` or `windows`.
|
||||
* @param {string} randUserAgent.device Name of a device, case-insensitive. `desktop`, `mobile` or `tablet`.
|
||||
* @returns A random useragent for the given specifications.
|
||||
*/
|
||||
module.exports = ({ browser = 'chrome', os = 'mac os', device = 'desktop' }) => {
|
||||
device = device.toLowerCase();
|
||||
browser = browser.toLowerCase();
|
||||
os = os.toLowerCase();
|
||||
let UA = randUserAgent(device, browser, os);
|
||||
|
||||
if (browser === 'chrome') {
|
||||
while (UA.includes('Chrome-Lighthouse') || UA.includes('Gener8') || UA.includes('HeadlessChrome') || UA.includes('SMTBot')) {
|
||||
UA = randUserAgent(device, browser, os);
|
||||
}
|
||||
}
|
||||
if (browser === 'safari') {
|
||||
while (UA.includes('Applebot')) {
|
||||
UA = randUserAgent(device, browser, os);
|
||||
}
|
||||
}
|
||||
return UA;
|
||||
};
|
||||
Reference in New Issue
Block a user