A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

94 line
3.8 KiB

  1. /*
  2. * Copyright © 2020-2021 Musing Studio LLC.
  3. *
  4. * This file is part of WriteFreely.
  5. *
  6. * WriteFreely is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, included
  8. * in the LICENSE file in this source code package.
  9. */
  10. let unlockingSplitContent = false;
  11. let unlockedSplitContent = false;
  12. let pendingSplitContent = false;
  13. function showWMPaywall($content, $split) {
  14. let $readmoreSell = document.createElement('div')
  15. $readmoreSell.id = 'readmore-sell';
  16. $content.insertAdjacentElement('beforeend', $readmoreSell);
  17. $readmoreSell.appendChild($split);
  18. $readmoreSell.insertAdjacentHTML("beforeend", '\n\n<p class="font sans">For <strong>$5 per month</strong>, you can read this and other great writing across our site and other websites that support Web Monetization.</p>')
  19. $readmoreSell.insertAdjacentHTML("beforeend", '\n\n<p class="font sans"><a href="https://coil.com/signup?ref=writefreely" class="btn cta" target="coil">Get started</a> <a href="https://coil.com/?ref=writefreely" class="btn cta secondary">Learn more</a></p>')
  20. }
  21. function initMonetization() {
  22. let $content = document.querySelector('.e-content')
  23. let $post = document.getElementById('post-body')
  24. let $split = $post.querySelector('.split')
  25. if (document.monetization === undefined || $split == null) {
  26. if ($split) {
  27. showWMPaywall($content, $split)
  28. }
  29. return
  30. }
  31. document.monetization.addEventListener('monetizationstop', function(event) {
  32. if (pendingSplitContent) {
  33. // We've seen the 'pending' activity, so we can assume things will work
  34. document.monetization.removeEventListener('monetizationstop', progressHandler)
  35. return
  36. }
  37. // We're getting 'stop' without ever starting, so display the paywall.
  38. showWMPaywall($content, $split)
  39. });
  40. document.monetization.addEventListener('monetizationpending', function (event) {
  41. pendingSplitContent = true
  42. })
  43. let progressHandler = function(event) {
  44. if (unlockedSplitContent) {
  45. document.monetization.removeEventListener('monetizationprogress', progressHandler)
  46. return
  47. }
  48. if (!unlockingSplitContent && !unlockedSplitContent) {
  49. unlockingSplitContent = true
  50. getSplitContent(event.detail.receipt, function (status, data) {
  51. unlockingSplitContent = false
  52. if (status == 200) {
  53. $split.textContent = "Your subscriber perks start here."
  54. $split.insertAdjacentHTML("afterend", "\n\n"+data.data.html_body)
  55. } else {
  56. $split.textContent = "Something went wrong while unlocking subscriber content."
  57. }
  58. unlockedSplitContent = true
  59. })
  60. }
  61. }
  62. function getSplitContent(receipt, callback) {
  63. let params = "receipt="+encodeURIComponent(receipt)
  64. let http = new XMLHttpRequest();
  65. http.open("POST", "/api/collections/" + window.collAlias + "/posts/" + window.postSlug + "/splitcontent", true);
  66. // Send the proper header information along with the request
  67. http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  68. http.onreadystatechange = function () {
  69. if (http.readyState == 4) {
  70. callback(http.status, JSON.parse(http.responseText));
  71. }
  72. }
  73. http.send(params);
  74. }
  75. document.monetization.addEventListener('monetizationstart', function() {
  76. if (!unlockedSplitContent) {
  77. $split.textContent = "Unlocking subscriber content..."
  78. }
  79. document.monetization.removeEventListener('monetizationstart', progressHandler)
  80. });
  81. document.monetization.addEventListener('monetizationprogress', progressHandler);
  82. }