1

I'm trying to make a browser extension to download our data from the Medium stats page.

In the console, I can access the data for each SVG rect in the bar graph, in their __data__ attributes. (I guess they're using D3).

Screenshot

But in a script injected via a Chrome extension, I can read the rest of the DOM fine, but the __data__ attributes are undefined.

(I tried changing the runAt property but that didn't have any effect).

I've replicated this with a small sample project here.

Do Chrome extension injected scripts not have access to data attached to the DOM?

Is there any workaround?

poshaughnessy
  • 1,978
  • 3
  • 21
  • 35
  • 4
    Content script are isolated so you need to [Insert code into the page context using a content script](//stackoverflow.com/a/9517879). Note, that's because `__data__` is an *expando property*, not an attribute. – wOxxOm Jul 03 '17 at 16:17
  • 2
    @wOxxOm Has it exactly right. I'll just add: the Chrome docs mention that extension scripts and in-page scripts "share the DOM" but in this case you can think of the DOM as a shared *state*, which you are given indirect read/write access to via DOM objects (`Element` instances, etc). Changes to these DOM-visibility objects are not visible across the extension/page boundary except when the changes happen to cause a change in shared DOM state. Changes to non-DOM-specified expando properties don't alter the shared DOM state. – apsillers Jul 03 '17 at 16:36
  • @wOxxOm Thanks. If I understand correctly though, I am inserting code using a content script - just using [programmatic injection](https://developer.chrome.com/extensions/content_scripts#pi) ([here's my code](https://github.com/poshaughnessy/chrome-extension-tests/tree/master/extension)) (because in the real version I'd like to make it happen upon pressing a button in the popup). Is it possible that content scripts can access expando properties? – poshaughnessy Jul 03 '17 at 16:40
  • @apsillers I just saw your comment after I posted the previous comment! I guess it's answered now then. It's not possible... :( Thanks! – poshaughnessy Jul 03 '17 at 16:42
  • 2
    It's possible. All you need is to insert a `script` element inside your content script with code that accesses the expando property and uses DOM messaging via CustomEvent to send the results back to the content script *as shown in the linked answer*. – wOxxOm Jul 03 '17 at 16:43
  • @wOxxOm Ahh sorry, I was confused by "using a content script". I get it now: I need the first script, to inject the second script! Thanks! – poshaughnessy Jul 03 '17 at 16:46
  • @wOxxOm Up to you but if you would like to write this as an answer, I can accept it as correct! :) – poshaughnessy Jul 03 '17 at 17:07
  • Possible duplicate of [Insert code into the page context using a content script](https://stackoverflow.com/questions/9515704/insert-code-into-the-page-context-using-a-content-script) – Makyen Jul 03 '17 at 20:55

0 Answers0