skip to Main Content

its been a long time since i post question on this website , i hope you guys are doing okay these days , now to the issue , i got this code that worked but not immediately:

const childElement = document.getElementById('m-x-content');
if (childElement && childElement.parentNode) {
    childElement.parentNode.remove();
}

I tried wrapping it with

document.addEventListener('DOMContentLoaded', () => {}

and it doesnt worked at all . Now the extensions i used : tampermonkey and runjavascript , both seems to only run the script fine but only after element is loaded .

Now i also already tried chatgpt suggestion about using and it didnt worked.

"… to use a bookmarklet or a custom browser extension to remove elements before they load, here’s a breakdown of how each approach can work:"

You can ask chapgpt and try it own your own .

so the question is , is this chrome/chromium specific issues , or thats just how browser work ? here is a link to a video that i recorded , you can see it in the last 15 sec that it didnt remove the element immedietly .

2

Answers


  1. Chosen as BEST ANSWER

    I revisit this issue and i found out custom browser extension solution , is the best solution for now , very fast performance compare to basic script injection , the element still get loaded but it get removed almost instantly , I think the real solution is to find and stop the script that responsible for loading the element in the first place , below is the code for custom browser extension solution:

    content.js

    // Use MutationObserver to remove the element as soon as it appears

    const observer = new MutationObserver(() => { const element = document.getElementById('m-x-content'); if (element && element.parentNode) { element.parentNode.remove(); observer.disconnect();

    observer.observe(document.documentElement, { childList: true, subtree: true });// Start observing immediately when the script loads // Stop observing after removal } });

    background.js

    chrome.runtime.onInstalled.addListener(() => {
    chrome.scripting.registerContentScripts([ { id: "removeElementScript", matches: ["<all_urls>"], js: ["content.js"], runAt: "document_start" } ]); });

    manifest.json

    { "manifest_version": 3, "name": "Auto Remove Element",
    "version": "1.0", "description": "Automatically removes specific elements from the page.", "permissions": ["scripting"],
    "host_permissions": ["<all_urls>"], "background": { "service_worker": "background.js" } }


  2. It happened to me also. It’s a new feature in recent chrome versions. Maybe security thing.
    A workaround:

    1. Go to chrome://flags/ in your Chrome browser.
    2. Search for "Enable (deprecated) synchronous mutation events" or simply "mutation".
    3. Set this flag to "Enabled".
    4. Restart your Chrome browser.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search