switch to doneEach and cache #main dom

This commit is contained in:
Leopoldthecoder 2017-03-07 11:30:09 +08:00
commit 9c54bafae4
8 changed files with 27 additions and 33 deletions

View file

@ -2,27 +2,22 @@ this.D = this.D || {};
(function () {
'use strict';
var scriptReg = /<script[^>]*src=["|'](.*)["|']>[^\w]*<\/script>/;
var asyncReg = /<script[^>]*\s+async/;
var deferReg = /<script[^>]*\s+defer/;
function handleExternalScript () {
var container = Docsify.dom.getNode('#main');
var script = Docsify.dom.find(container, 'script');
function handleExternalScript (html) {
var scriptMatch = html.match(scriptReg);
if (script && script.src) {
var newScript = document.createElement('script');['src', 'async', 'defer'].forEach(function (attribute) {
newScript[attribute] = script[attribute];
});
if (scriptMatch && scriptMatch.length > 1) {
var script = document.createElement('script');
script.src = scriptMatch[1];
if (asyncReg.test(scriptMatch[0])) { script.setAttribute('async', ''); }
if (deferReg.test(scriptMatch[0])) { script.setAttribute('defer', ''); }
var target = document.querySelector('#main');
target.appendChild(script);
script.parentNode.removeChild(script);
container.appendChild(newScript);
}
}
var install = function (hook) {
hook.afterEach(handleExternalScript);
hook.doneEach(handleExternalScript);
};
window.$docsify.plugins = [].concat(install, window.$docsify.plugins);