Remove elements with cloudflare workers

How to remove an element from html page using cloudflare workers?
Elements in the head or body
For example :
<meta name="robots" content="index, follow"> at head
<div class="footer"> some ... </div> at body

Can you help me with this

You need to use HTMLRewriter.




I’m using the following code to replace the site’s href, but I’m not using it to change the parameters

async function handleRequest(req) {
  const res = await fetch(req)
  return rewriter.transform(res)

 class AttributeRewriter {
  constructor(attributeName) {
    this.attributeName = attributeName

  element(element) {
    const attribute = element.getAttribute(this.attributeName)
    if (attribute) {
    .replace('/product/', '/p-')
    .replace('//', '//')
    .replace('//', '//')

inside html like the entries in the p tag
If you can help me how to remove <meta name="robots" content="index, follow"> or replace it with an empty phrase
Or, for example, how can I replace the expression in the p tag with

1 Like

I’m not familiar with the HTMLRewriter class, but the selector to catch your meta tag would be meta[name="robots"] similar to CSS selectors so you’d do something like:

new HTMLRewriter.on('meta[name="robots"]', new ElementHandler());

Check the links I provided for more info.

If you don’t feel like coding you can use the Cloudworker-Proxy that I put together to do the transform using config.

For removing the robots tag, add the following rule:

  const rules = [{
    "handlerName": "transform",
    "host": "",
    "path": "/optional-path.html",
    "options": {
      "transforms": [
          "regex": "<meta name=\"robots\" content=\"index, follow\">",
          "replace": ""

I am not familiar with these codes, if you may guide me based on the codes.
thank you

If you want to completely remove an element, just create a RemoveElement class and do it like this:

class RemoveElement {
    element(element) {

const rewriter = new HTMLRewriter()
    .on('meta[name="robots"]', new RemoveElement());