Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extension: how do I do modify DOM of a determinate page with contextMenus?

As title, how to be able to apply the modifications to another page, different from that of background?

Manifest:

{
    "name": "First Extension",
    "version": "1.0",
    "description": "The first extensione tha I made",
    "permissions":["contextMenus", "tabs", "http://localhost/CreativaIly/*"],
    "background_page": "background_page.html",
    "browser_action":{ 
                        "name": "My First Extension!",
                        "default_icon": "Chrome_icon32bn.png",
                        /*"default_popup": "popup.html",*/
                        "default_title": "My First Extension!"
    },
    "icons": {
                "16": "Chrome_icon19color.png",
                "48": "Chrome_icon32color.png",
                "128": "Chrome_icon.png"
    }
}

Background page:

<!doctype html>
<html>
  <head>
    <title>Background Page</title>
    <script type="text/javascript" src="mootools-core-1.4.2-full-compat.js"></script>
    <script src="script.js" type="text/javascript" ></script>
  </head>
  <body>
  </body>
</html>
like image 574
Donovant Avatar asked Dec 05 '25 02:12

Donovant


1 Answers

I strongly recommend reading about content scripts and message passing as the solution involves the combination of these 2 techniques.

I could do a quick solution for you using these techniques but you'd probably fall in to a pitfall later on because you didn't study these pages. However, I will give you a basic example flow;

  • Setup the manifest to inject a content script in to all pages that manipulates the DOM in the way you want when chrome.extension.onRequest is fired
  • Create your context menu and listen for click events
  • When onclick is fired capture the tab.id and use it to call chrome.tabs.sendRequest

It's a lot simpler than it may first appear.

like image 182
neocotic Avatar answered Dec 09 '25 13:12

neocotic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!