1

So there is a new API, proposed as a W3C standard, but to use it you need to have this extension for now.

The extension adds an additional document key named monetization, which you can access with document.monetization. And also, the website must have a payment pointer to be able to access it.

I'm trying to access it with an extension I am developing, but I get an undefined error. Here's my manifest.json

{
  "manifest_version": 2,
  "name": "Test Ext",
  "description": "Test Description",
  "version": "1.0.0",
  "browser_action": {
    "default_icon": "icon.png",
    "default_pop": "popup.html",
    "default_title": "A popup will come here."
  },
  "permissions": ["activeTab"],
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["app.js"]
    }
  ]
}

and in my app.js, I made a simple script to check if document.monetization is loaded.

const tid = setInterval( function () {
    if (document.monetization === undefined) return;
    console.log('Accessible', document.monetization);
    clearInterval( tid );       
}, 100 );

But it's not working. How do you manage this?

wobsoriano
  • 12,348
  • 24
  • 92
  • 162

1 Answers1

1

As we can see in the source code of that extension document.monetization is an expando property on a standard DOM document interface, this property is not a part of DOM, it's essentially a JavaScript object so it's not accessible directly from a content script which runs in an isolated world - all JavaScript objects/variables/expandos are isolated so the page scripts can't see the JS objects of content scripts and vice versa.

In Chrome to access such an expando property you need to run the code in page context and then use standard DOM messaging via CustomEvent to coordinate the code in page context and the content script as shown in a sibling answer in the same topic.

In Firefox you can use wrappedJSObject e.g. document.wrappedJSObject.monetization

wOxxOm
  • 65,848
  • 11
  • 132
  • 136