Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome App/extension to remove a page element? [closed]

I need to create a Chrome Extension to manipulate the HTML on some pages.

Basically, what I need to do is to remove a <div class="feedmain"> inside a page. I'm currently able to remove that <div> using the developer tools, but the element is obviously reloaded when I reload the page, so I would like to develop some extension that does this for me automatically.

So I would like to know if it is possible, and an overview of how it can be done in an extension.

like image 338
Chris S Avatar asked Jan 20 '15 13:01

Chris S


People also ask

How do I get rid of Chrome elements?

Activate the "select element" feature of the browser, move the mouse over the element you want to delete, and click on it to jump to it in the source code. Step 3. Right-click on the element in the code, and select "delete element", or press the Del-key directly, to remove it from the page.

How do I delete a Google element?

Right-click anything on the page and remove it. Useful if an ad is in the way of you reading an article. If you accidentally remove too much, just refresh the page - removing items is temporary.

How do you use CustomBlocker?

Click on the CustomBlocker extension icon, and then click Suggest under the Elements to Hide section. You can then easily click on any element that you want to block within a webpage, and it should be added as a rule automatically. You can also further refine filter rules using keywords.


1 Answers

Yes, absolutely, that's possible and pretty easy to do: all you need is a content script that will be injected into that particular page to remove the element for you.

To do what you want you'll need to:

  1. Create a simple empty Chrome Extension (see here for the official guide) with a manifest.json file.
  2. Specify the "content_scripts" field in the manifest.json, and declare the script you want to inject. Something like this:

    "content_scripts": [
        {
            "matches": ["http://somesite.com/somepage.html"],
            "js": ["/content_script.js"]
        }
    ]
    

    Take a look here to find out how content scripts work.

  3. Create a content_script.js, which will delete the element for you. This script will basically only contain the following two lines:

    var element = document.querySelector('div.feedmain');
    element.parentElement.removeChild(element);
    
like image 102
Marco Bonelli Avatar answered Sep 18 '22 17:09

Marco Bonelli