loading...

Web Monetization in Vue App using plugin

jasmin profile image Jasmin Virdi Updated on ・1 min read

Web Monetization (3 Part Series)

1) Video Web Component using Web Monetization API 2) Web Monetization in Vue App using plugin 3) Monetize your Electron App 💰

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);
  }
};
  • Register your plugin with your Vue App.
import Vue from "vue";
import { WebMonetizationPlugin } from "./web-monetization.js";

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

// to disable web monetization
this.$webMonetizaton.disableWebMonetization();

Link to code

You can find the code here

Resources

Web Monetization Doc
Vue js Plugins

Web Monetization (3 Part Series)

1) Video Web Component using Web Monetization API 2) Web Monetization in Vue App using plugin 3) Monetize your Electron App 💰

Posted on May 26 by:

Discussion

markdown guide
 

Soo, what does this webmonetizstion do, actually?

 

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