Proxy another website url on a particular path using cloudflare workers – modify response body

// cloudflare worker to proxy a path or a whole url and make changes in response for any use case. const SECOND_DOMAIN = 'second.YOURDOMAIN.com' const THIRD_DOMAIN = 'third.YOURDOMAIN.com' const THIRD_PATH = 'abc-pathname-seo' const THIRD_LINK = 'https://' + THIRD_DOMAIN + THIRD_PATH async function handleRequest(event) { const url = new URL(event.request.url) const pathname = url.pathname if (pathname === '/' + THIRD_PATH) { return thirdLander(event) } const search = url.search const pathWithParams = pathname + search return retrieveStatic(event, pathWithParams) } async function retrieveStatic(event, pathname) { let response = await caches.default.match(event.request) if (response) { return response } const requestHeaders = new Headers(event.request.headers) const headers = new Headers() headers.set('Host', SECOND_DOMAIN) headers.set('Accept', requestHeaders.get('Accept')) headers.set('Accept-Encoding', requestHeaders.get('Accept-Encoding')) let init = { redirect: 'manual', method: event.request.method, headers: headers, } response = await fetch(`https://${THIRD_DOMAIN}/${pathname}`, init) if (event.request.method === 'GET') { event.waitUntil(caches.default.put(event.request, response.clone())) } return response } async function thirdLander(event) { let response = await caches.default.match(event.request) if (response) { return response } const requestHeaders = new Headers(event.request.headers) const headers = new Headers() headers.set('Host', THIRD_DOMAIN) headers.set('Accept', requestHeaders.get('Accept')) headers.set('Accept-Encoding', requestHeaders.get('Accept-Encoding')) headers.set('Cookie', requestHeaders.get('Cookie')) let init = { redirect: 'manual', method: event.request.method, headers: headers, } response = await fetch(THIRD_LINK, init) // RESPONSE BODY MODIFICATION let res2 = response.clone() let orgBody = await response.text() orgBody = orgBody.replace(/src="\//g, 'src="') orgBody = orgBody.replace(/url\("\//g, `url("https://${THIRD_DOMAIN}/`) orgBody = orgBody.replace(/<\/head>/g, `<base href="https://${THIRD_DOMAIN}/" target=_blank><link rel="canonical" href="https://YOURDOMAIN.com${THIRD_PATH}" /></head>`) if (event.request.method === 'GET') { event.waitUntil(caches.default.put(event.request, new Response(orgBody, res2))) } return new Response(orgBody, res2) } addEventListener("fetch", (event) => { event.passThroughOnException() event.respondWith(handleRequest(event)) })

October 9, 2023 · 2 min · Harshvardhan Malpani

JS Notes Object.is vs === Triple EqualTo

Source: Linkedin

September 29, 2021 · 1 min · Harshvardhan Malpani

How to export multiple selected tables in PMA phpmyadmin (but not all tables)

Click on export, and choose “custom” export method. Lets say you want to export first 20 tables only. Open chrome or browser console [ctrl+shift+j], enter the following: $("input[name='table_data[]']").slice(0,20).click(); For next 20, use 20,50 as arguments in slice function used above.

May 25, 2021 · 1 min · Harshvardhan Malpani

A basic time difference calculator in google script js

In your Google Sheet, Click “Tools” and then choose “Script Editor“. Put the code given below. Don’t forget to use function in your result column as “=dodiff(cell1,cell2)“ function dodiff(a, b) { if (a) a = fix(a); if (b) b = fix(b); if (Array.isArray(a) && Array.isArray(b)) { if (b[1] > a[1]) m = b[1] - a[1]; else { m = b[1] - a[1] + 60; b[0]--; } if (b[0] >= a[0]) h = b[0] - a[0]; else { h = b[0] - a[0] + 12; } m = m / 60; return (h + m).toPrecision(4); } } function fix(a) { if (a) x = a.toString().replace(" ", '').replace("am", "").replace("pm", "").replace("PM", "").replace("AM", ""); x = x.split(":"); x[0] = parseInt(x[0]); x[1] = parseInt(x[1]); return x; }

October 15, 2019 · 1 min · Harshvardhan Malpani

How to track qty changes in cart?

This is not CMS or software specific. Can be used for shopify, magento, wordpress or any other cart page Assumption: All the cart items have an input field for quantity in case they want to change qty for any item Assumption: All those input fields can be selected using queryselector. Either they have common name attribute or a common class. Purpose: We want to change class of update button to show that he has to press update button because quantities have been altered. ...

July 21, 2019 · 1 min · Harshvardhan Malpani

Magento 2 Multi Select click all code – Tax Rates

Put the following code in console. require(["jquery"],function($){ $( ".mselect-fixed label" ).each(function( index ) { if($(this).children("input").hasClass("mselect-checked")){} else $(this).trigger('click'); }); });

April 26, 2019 · 1 min · Harshvardhan Malpani

5 ways to redirect your Web page?

Hi Everyone! In this post we will be learning about redirects. And see how you can redirect your web-page. We will see two ways to redirect web-pages: Through your registrars’ Cpanel. Right into your code. Through cPanel redirect feature You can easily redirect your visitors from one page to another with the help of the Redirects feature. To setup a redirection, access your website’s Control Panel and locate the Redirects menu. ...

January 12, 2019 · 2 min · Harshvardhan Malpani

ReactJS: Create your first react app.

React is one of the most in-demand and popular JavaScript library. And the reason is simple it gives you the ability to create complex User Interfaces with minimal and simplified code. In this article we are going to go through the installation process and the process of creating your first react app. Creating a react js app. There are a few different ways of creating a react app. But the simplest way is using a CDN – link. This allows you to start using ReactJS instantly, without any installation setup. ...

January 4, 2019 · 2 min · Harshvardhan Malpani

NodeJS: JavaScript powering the backend.

Node.js is an open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside of a browser. NodeJS is a JavaScript engine that you can install in your own system. Also, NodeJs makes it possible to use JavaScript in backend development as a backend programming language. (Cool! isn’t it.) You need to know this. Node isn’t a program that you simply launch like Word or Photoshop: you won’t find it pinned to the taskbar or in your list of Apps. To use Node you must type command-line instructions, so you need to be comfortable with (or at least know how to start) a command-line tool like the Windows Command Prompt, PowerShell, or the Git shell. ...

December 30, 2018 · 2 min · Harshvardhan Malpani

Understanding “this” keyword in JavaScript.

‘Hi Everyone! In this article, we are about to discuss **‘this’**keyword in JavaScript. It is a very important part of object-oriented JS programming. Hence it becomes crucial for serious developers to understand **‘this’**keyword. It is also one of the most confused concepts of JavaScript. What is ‘this’? Understanding 'this': To be able to understand what I am about to talk, you should be familiar with the basics of JavaScript. e.x – Variables, Objects, Functions, etc… ...

December 15, 2018 · 3 min · Harshvardhan Malpani