Updated sample.

Added option to force reload script.
This commit is contained in:
Mikael Svenson 2019-02-05 11:58:24 +01:00
parent 411c21ec40
commit 0a95601e37
3 changed files with 45 additions and 23 deletions

View File

@ -7,35 +7,44 @@ Script Editor Web Part, and allows you do drop arbitrary script or html on a mod
> Notice. All client-side web parts are deployed or enabled to be available in site level by tenant administrator using tenant app catalog. If there are concerns on enabling script options in a tenant, this web part or a approach should not be approved by tenant administrators.
As an example add the following scripts to the web part in order to show weather info on your page. First *jQuery* is loaded, then the *simpleWeather* extension, and finally the last script block is executed to show the weather.
As an example add the following scripts to the web part in order to show stock ticker info on your page. First *tv.js* is loaded, and then the script block is executed to show the ticker information.
```html
<div id="weather"></div>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery.simpleWeather/3.1.0/jquery.simpleWeather.min.js"></script>
<script>
jQuery.simpleWeather({
location: 'Oslo, Norway',
woeid: '',
unit: 'c',
success: function(weather) {
html = '<h2>'+weather.temp+'&deg;'+weather.units.temp+'</h2>';
html += '<ul><li>'+weather.city+', '+weather.region+'</li>';
html += '<li>'+weather.currently+'</li></ul>';
jQuery("#weather").html(html);
},
error: function(error) {
jQuery("#weather").html('<p>'+error+'</p>');
}
});
<!-- TradingView Widget BEGIN -->
<div class="tradingview-widget-container">
<div id="tradingview_ab4e5"></div>
<div class="tradingview-widget-copyright"><a href="https://www.tradingview.com/symbols/NASDAQ-MSFT/" rel="noopener" target="_blank"><span class="blue-text">MSFT chart</span></a> by TradingView</div>
</div>
<script type="text/javascript" src="https://s3.tradingview.com/tv.js"></script>
<script type="text/javascript">
new TradingView.widget({
"width": 400,
"height": 200,
"symbol": "NASDAQ:MSFT",
"interval": "D",
"timezone": "Etc/UTC",
"theme": "Light",
"style": "1",
"locale": "en",
"toolbar_bg": "#f1f3f6",
"enable_publishing": false,
"allow_symbol_change": true,
"container_id": "tradingview_ab4e5"
});
</script>
<!-- TradingView Widget END -->
```
The web part works by loading each script in a `<script src>` tag sequentially in the order they are specified, then any other `<script>` block is executed.
![site page header configurator web part](./assets/modern-script-editor-wp.gif)
If you for some reason need the script to force load on each new page, add the attribute `reload` to the script tag.
This will reload the script even on smart modern page loads.
```html
<script src="myscript.js" reload></script>
```
If all you want is to add markup on the page, you can do that as well. Adding the following html would show a headline and a list.
```html

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.4 MiB

After

Width:  |  Height:  |  Size: 10 MiB

View File

@ -30,7 +30,7 @@ export default class ScriptEditorWebPart extends BaseClientSideWebPart<IScriptEd
element.style.paddingTop = "0";
element.style.paddingBottom = "0";
element.style.marginTop = "0";
element.style.marginBottom = "0";
element.style.marginBottom = "0";
break;
}
element = element.parentElement;
@ -122,7 +122,6 @@ export default class ScriptEditorWebPart extends BaseClientSideWebPart<IScriptEd
return elem.nodeName && elem.nodeName.toUpperCase() === name.toUpperCase();
}
// Finds and executes scripts in a newly added element's body.
// Needed since innerHTML does not run scripts.
//
@ -150,10 +149,14 @@ export default class ScriptEditorWebPart extends BaseClientSideWebPart<IScriptEd
const urls = [];
const onLoads = [];
const forceReload = [];
for (let i = 0; scripts[i]; i++) {
const scriptTag = scripts[i];
if (scriptTag.src && scriptTag.src.length > 0) {
urls.push(scriptTag.src);
if (scriptTag.attributes["reload"]) {
forceReload.push(scriptTag.src);
}
}
if (scriptTag.onload && scriptTag.onload.length > 0) {
onLoads.push(scriptTag.onload);
@ -166,11 +169,21 @@ export default class ScriptEditorWebPart extends BaseClientSideWebPart<IScriptEd
window["define"].amd = null;
}
for (let i = 0; i < urls.length; i++) {
try {
const scriptUrl = urls[i];
try {
if (forceReload.indexOf(scriptUrl) !== -1) {
let hackReload = (<any>SPComponentLoader)._instance;
hackReload._systemJsLoader.delete(urls[i]);
}
} catch (silent) { }
await SPComponentLoader.loadScript(urls[i], { globalExportsName: "ScriptGlobal" });
} catch (error) {
console.error(error);
if (console.error) {
console.error(error);
}
}
}
if (oldamd) {