0

I am pretty new to Chrome extensions and to development in general.

I have a piece of code that I am often using during work that I just paste to the Chrome console to make it work.

Creating an extension to ease my work a little, I would like to have the same affect when clicking a link on my extension's pop up. Essentially, I am trying to send a "message" to the current tab's console from the popup. I tried to figure out how to send just a simple console.log("hi"), but could not get it to work or find a simple explanation for an idiot like myself.

Any help is greatly appreciated. Thanks!

Boris Ablamunits
  • 115
  • 1
  • 12
  • Need more info. Do you just need to log something to console? Does this something depend on anything in the page? If yes, what? – Xan Sep 22 '14 at 13:54
  • Hi Xan, thank you for the reply. The piece of code Im using does not depend on anything on the page. For now I think it will be good just to know how to log some text to the current tab's console, and maybe ill be able to take it from there.. – Boris Ablamunits Sep 22 '14 at 14:17

1 Answers1

2

See Architecture Overview. Depending on what you need to do, it's easy or not.

To interact with the current tab, at a minimum you need Content Scripts. Programmatic Injection is the most suitable for this case.

An absolute minimal extension would be:

manifest.json:

{
  /*..required fields like "name"..*/
  "background" : {
    "scripts" : [ "background.js" ]
  },
  "browser_action": {
    "default_icon": "someimage.png"
  },
  "permissions" : [
    "activeTab"
  ]
}

background.js:

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.executeScript(tab.id, {file: "content.js"});
});

content.js:

console.log("hi");

This achieves the task of printing a static string in the console. You can, if you need, make a popup page and move the logic from the onClicked listener to it, for instance if you need multiple links.


The possible complications are numerous.

  1. Suppose you want to print a string that depends on something known in the extension.

    Then, you have to employ Messaging or Storage to pass this information to the content script.

  2. Suppose you want to print something that depends on JavaScript running in the page (and not just DOM).

    Then, you have to inject your code into the page's context. See also Isolated context.

  3. Suppose you want to execute something in the console, instead of just logging.

    You can use the method 2 of executing the code in page's context, but at this point you should consider making a Devtools extension. You can add your own panels, interact with the page using eval() with full access to Console APIs. In general, this might be a good idea, if somewhat more complicated. Note that at the time of writing Devtools extensions are subject to a nasty bug.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206
  • Summoning the almighty RobW to triage the [bug](https://code.google.com/p/chromium/issues/detail?id=405096) mentioned. – Xan Sep 22 '14 at 14:54
  • This has been really helpful. Thanks for taking the time giving this detailed answer. – Boris Ablamunits Sep 22 '14 at 15:07
  • @Xan You should add an @-sign to ping me. Now I only saw that comment after visiting this page ;) – Rob W Sep 22 '14 at 22:04
  • @RobW I don't think it works if you haven't participated on a post. And I know you'll see it anyway ;) – Xan Sep 22 '14 at 22:06
  • Hi again Xan (And others:)- So I have managed to do a console.log in my current tab using the guidelines you have provided, Thanks! However, I am unable to step forward with executing the script itself. I have read about the various methods of injecting a script, but I could not understand what the best method would be if I would simply like to replicate whatever is happening when pasting the same script in the console (which works). I guess my question is - what exactly is happening when running code via the console, and how can I run the same code via an extension? – Boris Ablamunits Sep 25 '14 at 09:46
  • 1
    @BorisAblamunits The comment section is definitely not the place to discuss such questions; try to formulate this as a new question and post it. I monitor questions in this tag and will get back to you. – Xan Sep 25 '14 at 09:56