/****************************************************************************************/ // 1a. Get location path - replaces getting referrer via PHP var host_location = window.location.href; var host_url = new URL(host_location); var host_origin = String(host_url.origin); var host_path = String(host_url.pathname); // console.log("Content ID: "); // console.log("Pub Date: "); // console.log("Canonical: "); /****************************************************************************************/ // 1b. insert canonical link (function() { // Dynamically build the canonical URL from widget parameters var canonicalUrl = "https://comicskingdom.com/"; var link = document.createElement("link"); link.rel = "canonical"; link.href = canonicalUrl; // Append to head document.head.appendChild(link); })(); /****************************************************************************************/ // 1c. load iframe drawPortal(host_origin, host_path, "gannett"); /****************************************************************************************/ // 2. load jquery var jQueryScriptOutputted = false; initJQuery(); /****************************************************************************************/ // 3. load re-sizer plugin for host page (function (d, script) { script = d.createElement('script'); script.type = 'text/javascript'; script.async = true; script.onload = function () { // 4. init resizer // iFrameResize({log:false}); iFrameResize({ log: false }, '#kingportal') console.log() }; script.src = '//g-king.comicskingdom.net/v3/js/iframeResizer.min.js'; d.getElementsByTagName('body')[0].appendChild(script); }(document)); /****************************************************************************************/ /* STAR OF THE SHOW */ /****************************************************************************************/ /* Draws portal */ function drawPortal(this_origin, this_path, clientID, contentID) { // get args passed in URI var content_id = get_fr_uri("content_id"); var pubDate = get_fr_uri("pubdate"); var print_opt = get_fr_uri("print_opt"); if (contentID != null) { // puzzle can only be alacarte based on this request; ignore content_id var isAlacarte = "yes"; var content_id = contentID; } else { var isAlacarte = "no"; } // if it's a sample, default to first day of last month, if no month is provided if ((pubDate == null) && (clientID == "sample_alacarte")) { var tempDate = new Date(); var curYear = tempDate.getFullYear(); var curMonth = tempDate.getMonth(); var sampleDate = new Date(curYear, curMonth - 1, 0); sampleDate.setDate(sampleDate.getDate() + 1); // add a day pubDate = dateToString(sampleDate); } // set base iframe url; add clientID and a la carte opt var viewURL = ("//g-king.comicskingdom.net/v3/server/display.php?client_id=" + clientID + "&alacarte=" + isAlacarte); viewURL += ("&host_origin=" + this_origin); viewURL += ("&host_path=" + this_path); // if content_id is specified, check for pubDate; append viewURL as needed if (content_id != null) { if (pubDate != null) { viewURL += ("&pubdate=" + pubDate); } if (print_opt != null) { viewURL += ("&print_opt=" + print_opt); // DEPRICATE?? } viewURL += ("&content_id=" + content_id); } /******** Write iframe code to page ******/ /*NEW*/ // get special div if it's already present var puzzdiv = document.getElementById("comicwidget"); if (puzzdiv == null) { // create the special div var puzzdiv = document.createElement("div"); puzzdiv.setAttribute("id", "comicwidget"); var divCreated = true; } else { var divCreated = false; } // create the iframe inside div var kingiframe = document.createElement("iframe"); kingiframe.setAttribute("id", "kingportal"); kingiframe.setAttribute("name", "kingportal"); kingiframe.setAttribute("src", viewURL); kingiframe.setAttribute("width", "100%"); kingiframe.setAttribute("height", 600); // use to be 1500, but that's way too big kingiframe.setAttribute("scrolling", "no"); kingiframe.setAttribute("style", "border:0px"); // add iframe to div puzzdiv.appendChild(kingiframe); // get the current script in the DOM var kingscript = document.currentScript; // draw the div, placing it before the script element on the page if (divCreated) { kingscript.parentNode.insertBefore(puzzdiv, kingscript); } // -- Check for device type var ua = navigator.userAgent; var checker = { iphone: ua.match(/(iPhone|iPod|iPad)/), blackberry: ua.match(/BlackBerry/), android: ua.match(/Android/) }; if (checker.android || checker.iphone || checker.blackberry) { // scroll puzzle frame into view document.getElementById('kingportal').scrollIntoView(); } } /****************************************************************************************/ /* GENERAL FUNCTIONS */ /****************************************************************************************/ /* initialize jQuery if it's not present */ function initJQuery() { // if the jQuery object isn't available if (typeof (jQuery) == 'undefined') { if (!jQueryScriptOutputted) { //output the script (load it from jquery api) var script = document.createElement("SCRIPT"); script.async = true; script.type = 'text/javascript'; script.src = 'https://code.jquery.com/jquery-latest.min.js'; script.onload = function () { var $ = window.jQuery; }; document.getElementsByTagName("body")[0].appendChild(script); // output the script once. jQueryScriptOutputted = true; } setTimeout("initJQuery()", 50); } } /****************************************************************************************/ /* Parse individual arg from URI */ function get_fr_uri(name) { var q = unescape(location.search.substring(1)).split(/[=&]/); for (var j = 0; j < q.length; j += 2) { if (q[j] == name) { return q[j + 1]; } } return null; } /****************************************************************************************/ /* Convert date object to date string */ function dateToString(thisDate) { return (thisDate.getFullYear().toString() + twoDigitsZeroLead((thisDate.getMonth() + 1).toString()) + twoDigitsZeroLead(thisDate.getDate().toString())); } /****************************************************************************************/ /* Return zero-lead string if its length is less than 2 */ function twoDigitsZeroLead(stringValue) { if (stringValue.length >= 2) return stringValue; if (stringValue.length == 1) return ("0" + stringValue); return "00"; }