Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tltoulson/01236ca05273809e7ac3 to your computer and use it in GitHub Desktop.
Save tltoulson/01236ca05273809e7ac3 to your computer and use it in GitHub Desktop.
(function() {
var essUrl = 'ess';
var pollingDelay = 250;
var prevHeight;
var src = '';
var blacklist = [];
/**
* Executes the proper calculation strategy and returns the internal height of the iframe. You should edit this function if you find strategies that are not accounted for
* @function getHeight
*/
var getHeight = function() {
if (iframePageIs('com.glideapp.servicecatalog_checkout_view_v2.do')) {
return getTotalHeight([
'body > .outputmsg_div',
'body > table:nth-of-type(1)',
'#sc_order_status_intro_text',
'#sc_cart_view',
'.catalog_button_container'
], 0);
}
else if (iframePageIs('com.glideapp.servicecatalog_category_view.do')) {
return getTotalHeight([
'body'
], 0);
}
else if (iframePageIs('com.glideapp.servicecatalog_cat_item_view.do')) {
// resize catalog forms
return getTotalHeight([
'body > .outputmsg_div',
'body > table:nth-of-type(1)',
'#item_table',
'body > table:nth-of-type(3)'
], 100);
}
else if (iframePageIs('catalog_home.do')) {
return getTotalHeight([
'body > .outputmsg_div',
'body > table:nth-of-type(1)',
'#homepage_grid'
]);
}
else if (iframePageIs('$knowledge.do')) {
// Knowledge v3 UI
if ($('gsft_main').contentWindow.$j('body')[0].style.overflow != 'hidden') {
$('gsft_main').contentWindow.$j('body')[0].style.overflow = 'hidden';
}
// Shrink iframe when the src changes to force a recalculation
if (src != $('gsft_main').contentWindow.location.href) {
src = $('gsft_main').contentWindow.location.href;
return 400;
}
return getTotalHeight(['.application'], 100);
}
else if (iframePageIs('kb_view.do')) {
return getTotalHeight([
'body > .outputmsg_div',
'body > .navbar',
'.kb-view-content-wrapper',
'.snc-article-header-author',
'.snc-article-footer-section',
'.snc-article-footer'
], 100);
}
else if (iframePageIs('assessment_take2.do')) {
// Resize Assessments
return getTotalHeight([
'form'
], 100);
}
else if (iframePageIs('slushbucket.do')) {
// Resize Form Edit page
return getTotalHeight([
'body > .outputmsg_div',
'.section_header_div_no_scroll',
'#slushbucket_body'
], 100);
}
else if (iframePageIs('$pwd')) {
// Password Reset Pages, may need to adjust if statement for configured public URLs
return getTotalHeight([
'body'
], 100);
}
else if (iframePageIs('cms_admin_iframe.do')) {
return getTotalHeight([
'body'
], 0);
}
else if (iframePageIs('expert_shell.do')){
//Wizards
return getTotalHeight([
'body'
], 200);
}
else {
// resize default
return getTotalHeight([
'body > .outputmsg_div',
'.section_header_div_no_scroll',
'.form_body',
'.navbar-fixed-bottom',
'.tabs2_spacer',
'.tabs2_list'
], 100);
}
};
/**
* @function getTotalHeight
* @param {array} divs - Array of CSS selectors that identify the div's whose height should be added in the calculation
* @param {int} modifier - Number of additional pixels to add as a modifier
*/
var getTotalHeight = function(divs, modifier) {
var h = 0;
$j.each(divs, function(ix, val) {
h = h + $('gsft_main').contentWindow.$j(val).height();
});
return h + modifier;
};
/**
* Determines if the url contains the given fragment
* @function iframePageIs
* @param {string} urlFragment - String to search for in the iframe url
*
*/
var iframePageIs = function(urlFragment) {
return ($('gsft_main').contentWindow.location.href.indexOf(urlFragment) != -1);
};
/**
* A recursive polling function that gets the internal height of the iframe and resizes the it accordingly.
* @function resizeIframeFix
*/
var resizeIframeFix = function() {
setTimeout(function() {
var curHeight;
var i;
for (i = 0; i < blacklist.length; i++) {
if (iframePageIs(blacklist[i])) {
return;
}
}
if ($j && $('gsft_main') && $('gsft_main').contentWindow.$j) {
curHeight = getHeight();
if (prevHeight != curHeight) {
$j('#gsft_main').height(curHeight);
prevHeight = getHeight();
}
}
resizeIframeFix();
}, pollingDelay);
};
// Iniaite polling loop if we are on a top level CMS page
if (top == window && window.location.href.indexOf(essUrl) != -1) {
addAfterPageLoadedEvent(function() {
// if resizable iframe exists
if ($('gsft_main')) {
resizeIframeFix();
}
});
}
})();
@tltoulson
Copy link
Author

Originally I had two strategies, one that would run inside the iframe and another for Knowledge V3 that would calculate from outside the iframe. I have merged these into a single strategy for simplicity sake. Knowledge V3 UI would not work with the inside strategy so I chose the outside strategy. Basically, the polling loop:

  1. Checks the URL of the iframe's document
  2. Chooses the right calculation strategy for the URL
  3. Calculates the height of the iframe's document
  4. Sets the iframe height
  5. Recalculates the iframe document's height for a check in the next loop to prevent infinite growth of a page (Knowledge v3 in particular grows without this check, kudos to ServiceNow for helping me solve this part)
  6. Executes the polling loop function causing the loop to run again after a timeout

@tltoulson
Copy link
Author

Added support for resizing the iframe for Assessment Surveys

@tltoulson
Copy link
Author

Added support for Form and List configuring from the portal

@tltoulson
Copy link
Author

Added support for Password reset forms

@tltoulson
Copy link
Author

tltoulson commented Jun 13, 2016

Added support for Catalog Category page (com.glideapp.servicecatalog_category_view.do) with help from Shannon Burns

@tltoulson
Copy link
Author

Added support for kb_view.do and fixed some bugs with the outputmsg_div

@tltoulson
Copy link
Author

Added support for a blacklist of URLs and for the CMS Admin Iframe with help from Adam Thomson

@tltoulson
Copy link
Author

Added support for Wizards courtesy of Ben Collyer. Also confirmed that the script is still fully functional in Jakarta.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment