DEV Community

Jasmin Virdi
Jasmin Virdi

Posted on • Updated on

Web Monetization in Vue App using plugin

In this article I will be explaining how we can use plugins in Vue to extend the Web Monetization functionality to our Vue App.

Plugins are the most simple and easy way to add global features to our app. They can be used to extend additional capabilities like routing, immutable stores, analytics and DOM manipulations.

So let's try implementing Web Monetization using plugins in your Vue App.

Steps

  • Create a Web Monetization plugin.
// file: web-monetization.js

export default {
  install(Vue) {
    Vue.proptype.$webMonetizaton = this;
    Vue.webMonetizaton = this;

    if (typeof window !== "undefined") {
      window.$webMonetizaton = this;
    }
  },
  enableWebMonetization() {
    if (!document.monetized) {
      const monetizationTag = document.createElement("meta");
      monetizationTag.name = "monetization";
      monetizationTag.content = "payment_pointer";
      document.head.appendChild(monetizationTag);
    }
  },

  disableWebMonetization() {
    const removeMonetizationTag = document.querySelector(
      'meta[name="monetization"]'
    );
    removeMonetizationTag.remove();
  },

  webMonetizationEvents() {
    document.monetization.addEventListener("monetizationstart", currentState);
  },

  currentState(event) {
    //console.log(event);
  }
};
Enter fullscreen mode Exit fullscreen mode
  • Register your plugin with your Vue App.
import Vue from "vue";
import { WebMonetizationPlugin } from "./web-monetization.js";

Vue.use(WebMonetizationPlugin);
Enter fullscreen mode Exit fullscreen mode
  • Use plugin in your app.
//to enable web monetization
this.$webMonetizaton.enableWebMonetization();

// to disable web monetization
this.$webMonetizaton.disableWebMonetization();
Enter fullscreen mode Exit fullscreen mode

Link to code

You can find the code here

Resources

Web Monetization Doc
Vue js Plugins

Top comments (2)

Collapse
 
detzam profile image
webstuff

Soo, what does this webmonetizstion do, actually?

Collapse
 
jasmin profile image
Jasmin Virdi

Web Monetization is a javascript browser API which support payments.
Here is the documentation link for more info 😄