Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kanwei/6c5c4bcbd2bc432f2f66372de5b8234a to your computer and use it in GitHub Desktop.
Save kanwei/6c5c4bcbd2bc432f2f66372de5b8234a to your computer and use it in GitHub Desktop.
<script>
//Syracuse University
//Installing DTD tools for main donation form
//https://cusecommunity.syr.edu/s/1632/17/form/landing.aspx?sid=1632&gid=2&pgid=383&sitebuilder=1&contentbuilder=1
//Syracuse public API key
var DDCONF = {API_KEY: "YTQ4NDNhNTktYjIy"};
//loading the dtd script wipes out localstorage, so capture that data early
var dtd_streamlined_search_data = {};
if (localStorage){
if (localStorage.doublethedonation){
try{
var ss_data = JSON.parse(localStorage.doublethedonation);
dtd_streamlined_search_data.company_id = ss_data.doublethedonation_company_id;
dtd_streamlined_search_data.entered_text = ss_data.doublethedonation_entered_text;
}
catch(e){}
}
}
var process_dtd_code = function(){
//Remove legacy matching gift tool
setTimeout(function(){ jQuery("table.checkboxCategory td:contains('My employer will match my gift')").remove() }, 1000);
//Find out if we are on the donation page or finish page
var url_search = window.location.search;
var current_page = "";
if (url_search.includes("billing="))
current_page = "billing";
else if (url_search.includes("gfid="))
current_page = "donation";
else if (url_search.includes("showconf="))
current_page = "review";
else if (url_search.includes("fb_ciuid=") || url_search.includes("tid=") || url_search.includes("fshid="))
current_page = "finish";
//function that will execute on the donation page
var donation_page_function = function(){
var dtdbootstrapfn = function(){
if (window.doublethedonation){
doublethedonation.plugin.load_streamlined_input();
}
};
//try to put the streamlined search as a field element
var field_target = jQuery(".imod_categoryWrapper").first();
if (field_target.length){
var label = "<div class='imod_fieldLabel'><label>Matching Gift Company</label></div>";
var ss_input = "<div class='imod_fieldInput idbmsInstanceColumnInput'><div id='dd-company-name-input'></div></div>";
jQuery("<div class='imod_fieldWrapper'>" + label + ss_input + "</div>").insertAfter(field_target);
}
//else if (backup_target.length){
// jQuery( "<div>See if your company will match your donation!</div><div id='dd-company-name-input'></div><br><br>").insertBefore(backup_target);
//}
var script = document.createElement("script");
script.src = "https://doublethedonation.com/api/js/ddplugin.js";
document.head.appendChild(script);
jQuery('head').append('<link href="https://doublethedonation.com/api/css/ddplugin.css" rel="stylesheet" />');
script.onload = dtdbootstrapfn;
};
//function that will execute on the finish page
var finish_page_function = function(){
var dtdbootstrapfn = function(){
try{
if (window.doublethedonation) {
var formDataLabels = jQuery("div.imod_fieldLabel span");
var filter_hof = function(target){
return function(index, elem){
return elem.innerHTML.toUpperCase().includes(target.toUpperCase());
};
};
var donation_amount = [ jQuery("td.CommerceSubTotal").first().text(),
formDataLabels.filter(filter_hof("I would like to give")).first().siblings().first().text(),
formDataLabels.filter(filter_hof("Donation Amount")).first().siblings().first().text(),
formDataLabels.filter(filter_hof("Amount")).first().siblings().first().text()]
.filter(function(s){return s != ""}).shift();
var campaign = jQuery('title').text().replace(/^\s+|\s+$/gm,'') || "";
var email = formDataLabels.filter(filter_hof("Email")).first().parent().siblings().first().text();
var domain = doublethedonation.integrations.core.strip_domain(email);
var donation_identifier = jQuery(".PrimaryConfirmationNumber").children().first().text().split(/\W+/).pop() || (email + '' + Date.now());
var donor_first_name = formDataLabels.filter(filter_hof("Name")).first().parent().siblings().first().text();
var phone = formDataLabels.filter(filter_hof("Phone")).first().parent().siblings().first().text();
//load the plugin
var target = jQuery("div h3:contains('Transaction Summary')").parent()[0] || jQuery("div.CommerceSummary")[0];
jQuery( "<div id='dd-container'></div><br>" ).insertAfter(target);
doublethedonation.plugin.load_plugin();
doublethedonation.plugin.set_donation_identifier(donation_identifier);
doublethedonation.plugin.set_donation_campaign(campaign);
doublethedonation.plugin.email_domain( domain );
//register the donation
doublethedonation.integrations.core.register_donation({
"360matchpro_public_key": DDCONF.API_KEY,
"campaign": campaign,
"donation_identifier": donation_identifier,
"donation_amount": donation_amount,
"donor_phone": phone,
"donor_first_name": donor_first_name,
"donor_email": email,
"doublethedonation_company_id" : dtd_streamlined_search_data.company_id,
"doublethedonation_entered_text" : dtd_streamlined_search_data.entered_text
});
if (dtd_streamlined_search_data.company_id)
doublethedonation.plugin.set_company(dtd_streamlined_search_data.company_id);
}
}
catch(e){console.log('DTD error registering the donation' + e.toString())};
}
var script = document.createElement("script");
script.onload = dtdbootstrapfn;
script.src = "https://doublethedonation.com/api/js/ddplugin.js";
document.head.appendChild(script);
jQuery('head').append('<link href="https://doublethedonation.com/api/css/ddplugin.css" rel="stylesheet" />');
};
switch (current_page){
case "donation" : donation_page_function(); break;
case "finish" : finish_page_function(); break;
}
};
if (typeof jQuery !== 'undefined'){
jQuery( document ).ready(function() {
process_dtd_code();
});
}
else{
document.addEventListener('DOMContentLoaded', function(event){
process_dtd_code();
});
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment