22
loading...
This website collects cookies to deliver better user experience
$url.of.server/someID
. In order to monetize a page you should add a <meta>
tag on that page that contains this pointer:<meta name="monetization" content="$url.of.server/someID">
<meta>
tag — but in the future, when browsers support web monetization natively, they will do so themselves. If a page contains multiple monetization <meta>
tags the extension uses the first one and ignores the others.<meta>
tag. It's ugly, but it works:<meta name="monetization" content="$url.of.server/someID" id="paymentPointer">
function changePointer() {
let meta = document.querySelector('#paymentPointer');
meta.setProperty('content','$url.of.server/myOtherID');
}
<meta>
tag's content, as shown above. See this article for more information.<meta>
tag will likely change to a link tag, like this one:<link rel="monetization" content="https://url.of.server/someID">
<meta>
.<meta>
and the link tag. For that reason it would be nicer if the JavaScript API offered a direct, imperative way of setting the pointer, like:document.monetization.pointer = '$url.of.server/someOtherID';
document.monetization
container, three events, and one property.document.monetization
is the container for all API functionality, and its presence indicates that your current visitor supports monetization:if (document.monetization) {
// user may monetize you;
// find out and do something
} else {
// user is certain not to monetize you
}
document.monetization
is a <div>
DOM node that is not inserted into the document. Thus you can read document.monetization.nodeName
and most other DOM properties, even though there is no practical reason to do so.document.monetization.state
contains information about the current monetization state. It can take three values:started
: a monetization stream has started and you will receive money. At least one valid Interledger package has been received.pending
: a monetization stream has not yet started, but the extension is trying to connect.stopped
: no monetization stream possible: the page has no <meta>
tag, or the pointer is invalid.<meta>
tag is found the initial state is stopped
. If a <meta>
tag is present the initial state is pending
.<meta>
tag contains no valid payment pointer the state becomes stopped
. If a valid payment pointer is present the extension connects to the Interledger server and waits for the first package. Once that package arrives the state becomes started
.started
even if the connection drops — the extension keeps track of the time spent on the site, after all.pending
and then to either started
or stopped
, depending on the validity of the new pointer.if (document.monetization && document.monetization.state === 'started') {
// user is currently paying you
}
document.monetization
allows you to capture four events, three of which mirror the state
property pretty closely:monetizationpending
: a monetization stream is being started up, but is not sending payments yet. Fires when the state
is set to pending
.monetizationstart
: a monetization stream has started. Fires when the state
is set to started
.monetizationprogress
: a new single payment has arrived. See below.monetizationstop
: a monetization stream has stopped. Fires when the state
is set to stopped
.<meta>
tag is present. the monetizationpending
event will fire, followed by a monetizationstart
event and an indeterminate amount of monetizationprogress
events. If the payment pointer is invalid monetizationstop
fires.state
property, except that no event will fire if no <meta>
tag is present. The sequence restarts at pending
whenever you change the payment pointer.if (document.monetization) {
let extraContent = document.querySelector('#extraContent');
document.monetization.addEventListener('monetizationstart',function() {
extraContent.style.display = 'block';
// or any other way of showing content
});
document.monetization.addEventListener('monetizationstop',function() {
extraContent.style.display = 'none';
});
}
event.detail
.amount
is the amount contained in the current Interledger package, as an integer.assetCode
is a code for the currency, either a cryptocurrency or a real one.assetScale
is the number of places past the decimal for the amount. This serves to keep amount
an integer.paymentPointer
is the payment pointer the extension read from the <meta>
tag.receipt
is a proof of payment sent back to the payer.requestID
is a transient ID temporarily assigned to your payment stream.document.monetization.addEventListener('monetizationprogress',function(e){
let amt = e.detail.amount;
let scale = e.detail.assetScale;
let code = e.detail.assetCode;
let amount = amt * Math.pow(10,-scale);
let printableAmount = code + ' ' + amount;
// do something with amount or printableAmount
})
paymentPointer
contains the same information as the <meta>
tag. receipt
and requestID
contain values that are internal to the Interledger packages. You can use it to write a receipt verifyer if you like.amount
is 17 and assetScale
is 3 you received 17*10^-3, or 0.017, of the currency indicated in assetCode
.<meta>
tag and API. In the final part we’ll take a look at web monetization’s future, which includes a formal W3C standard.22