Last active
July 23, 2021 16:53
-
-
Save tltoulson/01236ca05273809e7ac3 to your computer and use it in GitHub Desktop.
A Global UI Script workaround for https://hi.service-now.com/kb_view.do?sysparm_article=KB0551949.
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
(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(); | |
} | |
}); | |
} | |
})(); |
Added support for resizing the iframe for Assessment Surveys
Added support for Form and List configuring from the portal
Added support for Password reset forms
Added support for Catalog Category page (com.glideapp.servicecatalog_category_view.do) with help from Shannon Burns
Added support for kb_view.do and fixed some bugs with the outputmsg_div
Added support for a blacklist of URLs and for the CMS Admin Iframe with help from Adam Thomson
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
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: