Last active
July 27, 2021 15:33
-
-
Save salsalabs/32fb0d83539010c3b63bd9d20ae508c7 to your computer and use it in GitHub Desktop.
Provide a way to fill the personal information into a page based on the type of page.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- BEGIN solution to autofill supporter information on most Salsa pages. --> | |
<? | |
// Server-side script to build an object containing the supporter fields to use | |
// on this page. The supporter key is retrieved from the "&k=" URL query. | |
// Nothing will appear if there is not a supporter_KEY or if the page key is not | |
// one that's supported by this solution. | |
// Caution! This script does not yet handle pages with short URLs. | |
var data = {}; | |
var pageType = 'unknown'; | |
var supporter_KEY = null; | |
(function() { | |
supporter_KEY = Request.getParameter('k'); | |
if (supporter_KEY == null) return; | |
var supporter = DB.getObject('supporter', supporter_KEY); | |
if (supporter == null) return; | |
var page = null; | |
var validPageTypes = 'action,donate_page,event,signup_page'.split(','); | |
validPageTypes.forEach(function(k) { | |
var key = Request.getParameter(k + '_KEY'); | |
if (key != null) { | |
pageType = k; | |
page = DB.getObject(k, key); | |
} else { | |
} | |
}); | |
if (page == null) return; | |
data = page.Request.split(',') | |
.map(function(t) { return t.trim(); }) | |
.filter(function(t) { return t.length != 0; }) | |
.reduce(function(a, t) { a[t] = supporter[t]; return a; }, {}); | |
data['supporter_KEY'] = supporter_KEY; | |
// If we get here, then we have a pageType and data. Yay! Time to add the scripts. | |
?> | |
<script type="text/javascript"> | |
function fillPage(data) { | |
Object.keys(data) | |
.forEach(function (k) { | |
var e = document.querySelectorAll('*[name="' + k + '"]'); | |
if (e.length != 0) { | |
Array.from(e).forEach(f => { | |
if (data[k]) { | |
if (f.type == "checkbox") { | |
f.click(); | |
} else { | |
f.value = data[k]; | |
} | |
} | |
}) | |
} | |
}) | |
var e = document.getElementById('tempAddress'); | |
if (e != null && data.hasOwnProperty('Street')) { | |
e.value = data['Street']; | |
} | |
e = document.getElementById('tempZip2'); | |
if (e != null && data.hasOwnProperty('Zip')) { | |
e.value = data['Zip'].split('-')[0]; | |
} | |
} | |
// Script to handle cases of the page type. Either fill the page | |
// or wait for the letter page on a targeted action and then fill. | |
function main() { | |
document.addEventListener('DOMContentLoaded', function () { | |
var pageType = <?= pageType.toJSON() ?>; | |
var data = <?= data.toJSON() ?>; | |
console.log(JSON.stringify(data)); | |
insertSupporterKey(data); | |
switch (pageType) { | |
case 'action': | |
// Fill the address and the zip on targeted actions, blind targeted actions and petitions. | |
fillPage(data); | |
// Wait for the letter page on targeted and multi-content targeted actions. | |
document.body.addEventListener('action-letter-page', function (e) { fillPage(data); }) | |
waitForLetterPage(); | |
break; | |
case 'unknown': | |
break; | |
default: | |
fillPage(data); | |
break; | |
} | |
}) | |
} | |
// Insert the supporter_KEY into the main form. That will make it possible for a supporter to | |
// change emails without creating a new record. | |
function insertSupporterKey(data) { | |
var f = document.querySelector(".salsa form"); | |
if (f == null) { | |
return; | |
} | |
//Silly Salsa can have several inputs named "key"... | |
var a = Array.from(f.querySelectorAll('input[name=key')); | |
if (a.length == 0) { | |
t = document.createElement('input'); | |
t.name = "key"; | |
t.type = "hidden"; | |
t.value = data['supporter_KEY']; | |
f.appendChild(t); | |
return; | |
} | |
a.forEach(function(t) { | |
t.value = data['supporter_KEY']; | |
}) | |
} | |
// Wait for the letter page in a targeted action, then fire a CustomEvent. | |
// Listeners should wait for event 'action-letter-page' from document.body | |
// document.body.addEventListener('action-letter-page', function (e) { your-modifier-function-here(); }) | |
function waitForLetterPage() { | |
var interval = setInterval(waitForLetterPageInner, 200); | |
function waitForLetterPageInner() { | |
var recipient = document.querySelector('.recipient'); | |
if (recipient != null) { | |
var event = new CustomEvent('action-letter-page', { detail: new Date().toISOString() }); | |
document.body.dispatchEvent(event); | |
clearInterval(interval); | |
} | |
} | |
} | |
// Fill the current page. | |
main(); | |
</script> | |
<? | |
// End of the SalsaScript. | |
})(); | |
?> | |
<!-- END solution to autofill supporter information on most Salsa pages. --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment