Mastodon verification on Google Sites with Cloudflare

How to verify any website you own using Cloudflare

cloudflare-google-sites-mastodon.png

If you use Cloudflare, you can use its Workers to modify the contents of a webpage. I have a website hosted on Google Sites, and its editor doesn’t allow me to include any tags needed to verify the website on Mastodon. So here is how I did it, using a combination of tutorials I found online.

It should work for any hosting service, not just Google Sites.

c1f75a5c23d47cec6d438f639f8f3e59.png

First, you need to create a Worker. To do that, on your main page, click on Workers & Pages, and then Overview:

9c79027b256bbf9feaf73c8c5bb6bf21.png

In Overview click on Create application

cfdf3e03bac1e29258e6f01caf4f35cd.png

Click on Create Worker

f9219e15846b24bd85d16c97eb6f1d79.png

Choose a name for your worker, like “mastodon-verify”. I have already created mine and I’m repeating the steps to take the screenshots for this tutorial, so I didn’t care about the name because I’m going to delete this.
Click on Deploy (you will change the code later).

def15779bfa6f741ca46ccc5b981d173.png

Click on Edit code.

9d81c3b86f0f5a6ca6495809c62cf3e2.png

This is the code you will replace. Delete it.

21bffcb2abec40ae4d2a3077d6936fef.png

Copy the code below and paste it in the place where you deleted the existing code:

/**
 * Welcome to Cloudflare Workers! This is your first worker.
 *
 * - Run "npm run dev" in your terminal to start a development server
 * - Open a browser tab at http://localhost:8787/ to see your worker in action
 * - Run "npm run deploy" to publish your worker
 *
 * Learn more at https://developers.cloudflare.com/workers/
 */

class ElementHandler {
  element(element) {
    element.append(`<link href="https://**[YOUR SERVER]**/@**[YOUR HANDLE]**" rel="me">`, {html: true});
  }
}

async function handleRequest(req) {
  const res = await fetch(req)
  if (!res.url.includes('ads.txt')) {
    return new HTMLRewriter().on("head", new ElementHandler()).transform(res)
  }  
  return res;
}

addEventListener("fetch", (event) => {
  event.respondWith(
    handleRequest(event.request).catch(
      (err) => new Response(err.stack, { status: 500 })
    )
  );
});

On the top right corner, click on Save and deploy.

1631aba5b834681aa762de4d0c8dfc3e.png

Go to your domain configration page and click on Workers Routes on the sidebar.

69d4a18611b315f5e16dad9dbb6e4f7c.png

Click on Add route.

71f05bfec5e86ae0f8baa75e76c32c56.png

You will write *your-domain. Don’t forget the asterisk in the beginning. If your domain is avocado.com you will write *avocado.com.
Select your worker on the dropdown. If you named it mastodon-verify, that’s what you will select.
Click Save.

6d74c7dec489ede23203ce586b1441cb.png

Wait a few seconds and it should be working. You might need to go to your Mastodon profile, edit and save it again.

Cheers!