Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Commit

Permalink
push <template /> parsing for web monetization meta tag
Browse files Browse the repository at this point in the history
  • Loading branch information
totorogendut committed Mar 24, 2020
1 parent de2af3f commit 53d8c94
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Currently it is still rather new and only support Web Monetization API, along wi

- [ ] Make a JAMstack website to host documentation
- [ ] Early and basic asynchronous ads support (like amp-ads)
- [ ] Integrate basic cookies prompt flow and make ads cookie aware if possible (by using non-personalized ads)
- [ ] More robust API!

## Disclaimer
Expand Down
50 changes: 49 additions & 1 deletion dist/fundme-amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ define(['exports'], function (exports) { 'use strict';
function weightIsNotANumber(str) {
return `Fundme.js: ${str} has weight that is not a number. It has been set to ${DEFAULT_WEIGHT} (default).`;
}
// pointers template
const noTemplateFound = 'Fundme.js: no monetization template is found.';
const noDataFundIsFound = 'Fundme.js: <template /> has no data-fund attribute to parse.';
const templateSinglePointerHasWeight = 'Fundme.js: found single <template data-fund /> but has weight - only address will be parsed.';

const DEFAULT_WEIGHT = 5;
// TODO check pointer.address with RegEx
function setPointerMultiple(pointers, maxPool) {
function setPointerMultiple(pointers) {
const pool = createPool(pointers);
const pickedPointer = pickPointer(pool);
setWebMonetizationPointer(getPointerAddress(pickedPointer));
Expand Down Expand Up @@ -102,6 +106,49 @@ define(['exports'], function (exports) { 'use strict';
return pointer;
}

function setPointerFromTemplates() {
const pointers = scrapeTemplate();
if (pointers.length > 1) {
setPointerMultiple(pointers);
}
else if (pointers.length === 1) {
setPointerSingle(pointers[0].address);
if (typeof pointers[0] !== 'string') {
console.warn(templateSinglePointerHasWeight);
}
}
else {
console.warn('Pointer from template is undefined.');
}
}
function scrapeTemplate() {
const templates = document.body.querySelectorAll('template[data-fund]');
let pointers = [];
if (templates.length > 0) {
templates.forEach(template => {
const pointer = parseTemplate(template);
pointers = [...pointers, pointer];
});
}
else {
throw new Error(noTemplateFound);
}
return pointers;
}
function parseTemplate(template) {
let address = template.dataset.fund;
let weight = parseInt(template.dataset.fundWeight);
if (!address) {
throw new Error(noDataFundIsFound);
}
const pointer = checkWeight({
address,
weight
});
console.table(pointer);
return pointer;
}

let defaultAddress;
var FundType;
(function (FundType) {
Expand Down Expand Up @@ -136,6 +183,7 @@ define(['exports'], function (exports) { 'use strict';
return FundType.isMultiple;
}
if (pointer === undefined) {
setPointerFromTemplates();
return FundType.isFromTemplate;
}
throw new Error(invalidAddress);
Expand Down
2 changes: 1 addition & 1 deletion dist/fundme-amd.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/fundme-cjs-compat.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 49 additions & 1 deletion dist/fundme-cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,14 @@ const addressIsNotAString = "Fundme.js: address must be a string.";
function weightIsNotANumber(str) {
return `Fundme.js: ${str} has weight that is not a number. It has been set to ${DEFAULT_WEIGHT} (default).`;
}
// pointers template
const noTemplateFound = 'Fundme.js: no monetization template is found.';
const noDataFundIsFound = 'Fundme.js: <template /> has no data-fund attribute to parse.';
const templateSinglePointerHasWeight = 'Fundme.js: found single <template data-fund /> but has weight - only address will be parsed.';

const DEFAULT_WEIGHT = 5;
// TODO check pointer.address with RegEx
function setPointerMultiple(pointers, maxPool) {
function setPointerMultiple(pointers) {
const pool = createPool(pointers);
const pickedPointer = pickPointer(pool);
setWebMonetizationPointer(getPointerAddress(pickedPointer));
Expand Down Expand Up @@ -104,6 +108,49 @@ function convertToPointer(str) {
return pointer;
}

function setPointerFromTemplates() {
const pointers = scrapeTemplate();
if (pointers.length > 1) {
setPointerMultiple(pointers);
}
else if (pointers.length === 1) {
setPointerSingle(pointers[0].address);
if (typeof pointers[0] !== 'string') {
console.warn(templateSinglePointerHasWeight);
}
}
else {
console.warn('Pointer from template is undefined.');
}
}
function scrapeTemplate() {
const templates = document.body.querySelectorAll('template[data-fund]');
let pointers = [];
if (templates.length > 0) {
templates.forEach(template => {
const pointer = parseTemplate(template);
pointers = [...pointers, pointer];
});
}
else {
throw new Error(noTemplateFound);
}
return pointers;
}
function parseTemplate(template) {
let address = template.dataset.fund;
let weight = parseInt(template.dataset.fundWeight);
if (!address) {
throw new Error(noDataFundIsFound);
}
const pointer = checkWeight({
address,
weight
});
console.table(pointer);
return pointer;
}

let defaultAddress;
var FundType;
(function (FundType) {
Expand Down Expand Up @@ -138,6 +185,7 @@ function fund(pointer, options) {
return FundType.isMultiple;
}
if (pointer === undefined) {
setPointerFromTemplates();
return FundType.isFromTemplate;
}
throw new Error(invalidAddress);
Expand Down
Loading

0 comments on commit 53d8c94

Please sign in to comment.