DEV Community

Jasmin Virdi
Jasmin Virdi Subscriber

Posted on Edited 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);
  }
};
📺 Info Siaran: jasmin/web monetization in vue app using plugin - Jadwal Siaran Langsung 📺 Info Siaran: jasmin/web monetization in vue app using plugin - Jadwal Siaran Langsung
  • Register your plugin with your Vue App.
import Vue from "vue";
import { WebMonetizationPlugin } from "./web-monetization.js";

Vue.use(WebMonetizationPlugin);
📺 Info Siaran: jasmin/web monetization in vue app using plugin - Jadwal Siaran Langsung 📺 Info Siaran: jasmin/web monetization in vue app using plugin - Jadwal Siaran Langsung
  • Use plugin in your app.
//to enable web monetization
this.$webMonetizaton.enableWebMonetization();

// to disable web monetization
this.$webMonetizaton.disableWebMonetization();
📺 Info Siaran: jasmin/web monetization in vue app using plugin - Jadwal Siaran Langsung 📺 Info Siaran: jasmin/web monetization in vue app using plugin - Jadwal Siaran Langsung

Link to code

You can find the code here

Resources

Web Monetization Doc
Vue js Plugins

Top comments (2)

📺 Info Siaran: jasmin/web monetization in vue app using plugin - Jadwal Siaran Langsung
 
detzam profile image
webstuff •

Soo, what does this webmonetizstion do, actually?

📺 Info Siaran: jasmin/web monetization in vue app using plugin - Jadwal Siaran Langsung
 
jasmin profile image
Jasmin Virdi •

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