▌m=1 parametresi kaldırma
/**
* Environment interface
*
* @typedef Env
* @property {string} my_var
*/
// constants
const MOBILE_REGEX = /(?:phone|windows\s+phone|ipod|blackberry|(?:android|bb\d+|meego|silk|googlebot) .+? mobile|palm|windows\s+ce|opera\ mini|avantgo|mobilesafari|docomo|KAIOS)/i;
const TABLET_REGEX = /(?:ipad|playbook|(?:android|bb\d+|meego|silk)(?! .+? mobile))/i;
/**
* A helper function to get the device type from user-agent
*
* @param {string | null} userAgent
*
* @returns {"mobile" | "tablet" | "desktop"}
*/
const getDeviceType = (userAgent) => {
if (typeof userAgent === "string") {
if (MOBILE_REGEX.test(userAgent)) {
return "mobile";
}
if (TABLET_REGEX.test(userAgent)) {
return "tablet";
}
}
// Everything else not matched above will be considered as desktop
return "desktop";
}
/**
* An object with workers handlers
*
* @type {ExportedHandler}
*/
const worker = {
async fetch(request, env, context) {
// Get the device type from user-agent header
const deviceType = getDeviceType(request.headers.get("User-Agent"));
const proxiedUrl = new URL(request.url);
// Set the search param 'm' with value '1' if the device type is not 'desktop'
if (deviceType !== "desktop") {
proxiedUrl.searchParams.set("m", "1")
}
const proxiedRequest = new Request(proxiedUrl, {
method: request.method,
body: request.body,
headers: request.headers,
redirect: "follow"
});
const proxiedResponse = await fetch(proxiedRequest);
const response = new Response(proxiedResponse.body, proxiedResponse);
// OPTIONAL: You can further modify the response here :)
return response;
}
}
// Export handlers
export default worker;