During our research we had a recurring thought cross our mind: How do I test my project? How do I know my site is actually going to detect user's payments and do what it is supposed to?
Coil kindly offered a free trial to all the participants of this challenge. But we felt that this was not enough. There was no way to control how much money Coil will send, or how frequent it sends it. We pictured a tool for the developers to control how to trigger these events, so they could test all the different use cases they developed.
That's how we ended creating Web Monetization Simulator, a browser extension to simulate a web monetization payment provider. Give it a try:
First thing we had to decide was: Firefox or Chrome? We did a little digging and found out that although both implement basically the same API, Firefox does it using the browser namespace and Promises and Chrome uses the chrome namespace and callbacks. So which one to choose?
Firefox also implements these APIs under the chrome namespace using callbacks. This allows code written for Chrome to run largely unchanged in Firefox for the APIs documented here.
Thankfully the great people at Firefox implement also the chrome namespace API, so we chose to focus on Chrome and hope it just worked in Firefox, which it did 😄
The different JavaScript contexts
One thing we found out early on was that thanks to the isolation between the contexts of the extension and the actual page you are seeing, adding the document.monetizationexpando was not an easy task.
After some research we found out we needed to inject the site with our own JavaScript snippet that would handle the following things for the extension:
Creating the document.monetization expando
Modifying document.monetization.state
Dispatching our monetization events
constscript=`
document.monetization = new EventTarget();
document.monetization.state = "stopped";
window.addEventListener("message", function(event) {
// We only accept messages from ourselves
if (event.source != window)
return;
if (event.data.type === "monetizationEvent") {
const payload = event.data.event
event = new CustomEvent(payload.type, { detail: payload.detail });
document.monetization.dispatchEvent(event);
return;
}
if (event.data.type === "monetizationStateChange") {
document.monetization.state = event.data.state
return;
}
}, false);
`;constscriptElement=document.createElement("script");scriptElement.innerHTML=script;(document.head||document.documentElement).appendChild(scriptElement);
An instance for each page
One requirement for our extension was the ability to check multiple pages at the same time because that's how we browse the web.
This presents a challenge because some of the extension's components (background and popup scripts) are "global", theres only one instance of them running. On the other hand, the content script runs an instance per page.
To handle this we made the content script keep all the state and business logic. The popup script would just send data or ask for it using the messaging API.