Skip to content

Commit 8986a78

Browse files
committed
allow reload_scripts to also reload <script> tags by id.
1 parent 9ed9c8a commit 8986a78

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,10 +499,13 @@ You can set `reload_scripts:` in your `mkdocs.yml` with list of script source, t
499499
plugins:
500500
- encryptcontent:
501501
reload_scripts:
502-
- "./js/example.js"
502+
- 'js/example.js'
503+
- '#autoexec'
503504
```
504505

505-
This feature now doesn't use JQuery anymore (related to [issue #30](https://github.com/unverbuggt/mkdocs-encryptcontent-plugin/issues/30)).
506+
This feature now doesn't use JQuery anymore.
507+
508+
It is also possible to reload a script id like `<script id="autoexec">console.log('test');</script>` that was encrypted within the page (related to [issue #30](https://github.com/unverbuggt/mkdocs-encryptcontent-plugin/issues/30)).
506509

507510
### Self-host crypto-js
508511

encryptcontent/decrypt-contents.tpl.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,28 @@ function reload_js(src) {
132132
let script_src, script_tag, new_script_tag;
133133
let head = document.getElementsByTagName('head')[0];
134134

135-
if (base_url == '.') {
136-
script_src = src;
135+
if (src.startsWith('#')) {
136+
script_tag = document.getElementById(src.substr(1));
137+
if (script_tag) {
138+
script_tag.remove();
139+
new_script_tag = document.createElement('script');
140+
new_script_tag.innerHTML = script_tag.innerHTML;
141+
head.appendChild(new_script_tag);
142+
}
137143
} else {
138-
script_src = base_url + '/' + src;
139-
}
144+
if (base_url == '.') {
145+
script_src = src;
146+
} else {
147+
script_src = base_url + '/' + src;
148+
}
140149

141-
script_tag = document.querySelector('script[src="' + script_src + '"]');
142-
if (script_tag) {
143-
script_tag.remove();
144-
new_script_tag = document.createElement('script');
145-
new_script_tag.src = script_src;
146-
head.appendChild(new_script_tag);
150+
script_tag = document.querySelector('script[src="' + script_src + '"]');
151+
if (script_tag) {
152+
script_tag.remove();
153+
new_script_tag = document.createElement('script');
154+
new_script_tag.src = script_src;
155+
head.appendChild(new_script_tag);
156+
}
147157
}
148158
};
149159

0 commit comments

Comments
 (0)