Skip to content

Instantly share code, notes, and snippets.

@samrit
Last active April 19, 2018 02:23
Show Gist options
  • Save samrit/8fac88c09e9ecb63e8ba18e8c5b62ed8 to your computer and use it in GitHub Desktop.
Save samrit/8fac88c09e9ecb63e8ba18e8c5b62ed8 to your computer and use it in GitHub Desktop.
AJC Sponsorship Script
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).ajcSponsors=e()}}(function(){return function e(t,n,o){function r(i,l){if(!n[i]){if(!t[i]){var a="function"==typeof require&&require;if(!l&&a)return a(i,!0);if(s)return s(i,!0);var c=new Error("Cannot find module '"+i+"'");throw c.code="MODULE_NOT_FOUND",c}var d=n[i]={exports:{}};t[i][0].call(d.exports,function(e){var n=t[i][1][e];return r(n||e)},d,d.exports,e,t,n,o)}return n[i].exports}for(var s="function"==typeof require&&require,i=0;i<o.length;i++)r(o[i]);return r}({1:[function(e,t,n){"use strict";var o=this&&this.__awaiter||function(e,t,n,o){return new(n||(n=Promise))(function(r,s){function i(e){try{a(o.next(e))}catch(e){s(e)}}function l(e){try{a(o.throw(e))}catch(e){s(e)}}function a(e){e.done?r(e.value):new n(function(t){t(e.value)}).then(i,l)}a((o=o.apply(e,t||[])).next())})};Object.defineProperty(n,"__esModule",{value:!0});const r=e("../models/sponsorship"),s=e("../models/banner"),i=e("../models/article"),l=e("../models/header"),a=e("../logger");class c{constructor(e,t,n){this.sheetId=e,this.sheetName=t,this.apiKey=n,this.sponsorSearchUrl=`https://sheets.googleapis.com/v4/spreadsheets/${e}/values/${t}!A:A?key=${n}`,a.log(`Search url is ${this.sponsorSearchUrl}`),this.sponsorDataUrl=`https://sheets.googleapis.com/v4/spreadsheets/${e}/values/${t}!{0}?key=${n}`,a.log(`Search url is ${this.sponsorDataUrl}`)}getSponsorship(e){return o(this,void 0,void 0,function*(){a.log("Querying google spreadsheet");const t=yield(yield fetch(this.sponsorSearchUrl)).json();a.log(`Sponsor search:\n${JSON.stringify(t,null,2)}`);let n=0;for(n;n<t.values.length;n++){const o=t.values[n];if(o.length>0&&o[0]===e)break}if(a.log(`Sponsor Index is ${n}`),n===t.values.length)return void a.log(`Could not find sponsor ${e}`);const o=n+1;a.log(`Fetching data from B${o}:K${o}`);const c=yield(yield fetch(this.sponsorDataUrl.replace("{0}",`B${o}:L${o}`))).json();if(0===c.values.length)return void a.log("No values returned");const d=c.values[0];if(d.length<11)return void a.log("Invalid data in spreadsheet");const u=new s.Banner(d[0],d[1]),h=[];for(let e=5;e<11;e+=2)h.push(new i.Article(d[e],d[e+1]));const p=new l.Header(d[2],d[3],d[4],h);return new r.Sponsorship(u,p)})}}n.Sheet=c},{"../logger":2,"../models/article":4,"../models/banner":5,"../models/header":6,"../models/sponsorship":7}],2:[function(e,t,n){"use strict";var o=this&&this.__awaiter||function(e,t,n,o){return new(n||(n=Promise))(function(r,s){function i(e){try{a(o.next(e))}catch(e){s(e)}}function l(e){try{a(o.throw(e))}catch(e){s(e)}}function a(e){e.done?r(e.value):new n(function(t){t(e.value)}).then(i,l)}a((o=o.apply(e,t||[])).next())})};Object.defineProperty(n,"__esModule",{value:!0}),n.log=function(e){return o(this,void 0,void 0,function*(){})}},{}],3:[function(e,t,n){"use strict";var o=this&&this.__awaiter||function(e,t,n,o){return new(n||(n=Promise))(function(r,s){function i(e){try{a(o.next(e))}catch(e){s(e)}}function l(e){try{a(o.throw(e))}catch(e){s(e)}}function a(e){e.done?r(e.value):new n(function(t){t(e.value)}).then(i,l)}a((o=o.apply(e,t||[])).next())})};Object.defineProperty(n,"__esModule",{value:!0});const r=e("./data/sheet"),s=e("./logger"),i="1RBp7t1F6OGINh7rK3qOT4hBoio30OB5d9WpBbRql6zM",l="Sheet1",a="AIzaSyDwbTfRGdrCM3SBDHJ2HKdbqHSD-mS1d8M";n.loadSponsorship=function(e){return o(this,void 0,void 0,function*(){s.log("Loading sponsorships");const t=yield new r.Sheet(i,l,a).getSponsorship(e);t.renderBanner(),t.renderArticles()})}},{"./data/sheet":1,"./logger":2}],4:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0});const o=e("../logger");class r{constructor(e,t){this.articleText=e,this.articleUrl=t,o.log(`Found article ${this.articleText}, ${this.articleUrl}`)}getHtmlElement(){const e=document.createElement("p"),t=document.createElement("a");return t.href=this.articleUrl,t.innerHTML=`<b>${this.articleText}</b>`,e.appendChild(t),e}}n.Article=r},{"../logger":2}],5:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0});const o=e("../logger");class r{constructor(e,t){this.imageUrl=e,this.href=t,o.log(`Found banner ${this.imageUrl}, ${this.href}`)}getHtmlElement(){const e=document.createElement("div"),t=document.createElement("a");t.href=this.href;const n=document.createElement("img");return n.src=this.imageUrl,n.setAttribute("style","width: 100%"),t.appendChild(n),e.appendChild(t),e}}n.Banner=r},{"../logger":2}],6:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0});const o=e("../logger");class r{constructor(e,t,n,r){this.headerText=e,this.sponsorLogoUrl=t,this.sponsorLogoHref=n,this.articles=r,o.log(`Found header text ${this.headerText}`),o.log(`Found sponsor logo ${this.sponsorLogoUrl}`)}getHtmlElement(){const e=document.createElement("div");e.classList.add("row"),e.setAttribute("style","border: 1px solid rgb(225, 232, 237); padding: 8px; margin-bottom: 4px; display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; align-items: center;");const t=document.createElement("div");t.classList.add("col-md-9"),t.classList.add("col-sm-12");const n=document.createElement("h4");n.innerText=this.headerText,t.appendChild(n),t.appendChild(document.createElement("br")),this.articles.forEach(e=>{t.appendChild(e.getHtmlElement())});const o=document.createElement("div");o.classList.add("col-md-3"),o.classList.add("col-sm-12"),o.setAttribute("style","text-align:center; vertical-align: middle; display: inline-block; float: none;");const r=document.createElement("img");r.src=this.sponsorLogoUrl,r.height=120,r.width=120;const s=document.createElement("a");return s.href=this.sponsorLogoHref,s.target="_blank",s.appendChild(r),o.appendChild(s),e.appendChild(t),e.appendChild(o),e}}n.Header=r},{"../logger":2}],7:[function(e,t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0});const o=e("../logger");class r{constructor(e,t){this.banner=e,this.header=t}renderBanner(){const e=document.querySelector("header.story-header > div.lead-object > div.lead-video");e?(o.log("Inserting banner"),e.insertBefore(this.banner.getHtmlElement(),e.firstChild)):o.log("Could not find parent DOM element for banner")}renderArticles(){const e=document.querySelector("div.story-text > p:nth-child(4)");e?(o.log("Inserting header"),e.insertAdjacentElement("afterend",this.header.getHtmlElement())):o.log("Could not find sibling for articles")}}n.Sponsorship=r},{"../logger":2}]},{},[3])(3)});
//# sourceMappingURL=ajcSponsors.js.map
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment