The customary <script type="text/template"> tag for client-side processing isn't sufficient for my purposes because it isn't nestable. So I'm trying to extend it to work around this limitation.
Basically I have some raw html with embedded images that I want to parse the content client-side using javascript. I like the script tag because images inside it are not loaded by the browser. But when the html contains script tags, they break my application. Here's how I'm trying to extend the tag:
<script>
var scriptData = document.registerElement('script-data', {
prototype: HTMLScriptElement.prototype
});
captureStart();
</script>
When executed, chrome throws the following error:
Uncaught NotSupportedError: Failed to execute 'registerElement' on 'Document': Registration failed for type 'script-data'. The prototype is already in-use as an interface prototype object.
Update:
Disputing my intentions or suggesting alternative methods is fine, however I still want an answer to my initial question: how does one extend the script tag?
How I'm using this is actually more complicated. I'm basically using this to "capture" the whole body of my HTML document, so that I can manipulate things before they are displayed. It's unconventional, I know. But I'm exploring it as a type of academic study.
The benefits to this approach include:
- The html validates
- The code is light-weight, and I hope better supported than solutions like mobify.js
- I am able to modify content before the browser loads image resources
The challenges to this approach include:
- The body of the document can have 3rd party modules/widgets with script tags or html comments.
- I can't escape inline script tags. They need to remain in-tact because browsers with javascript disabled will display the document normally.
My proposed solution to these problems:
- Extending the prototype of the script tag would allow me to use a custom tag that I know will never appear in other components.
Here's a larger sample of the code:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Script Test</title>
<style>
html, body {
margin: 0;
padding: 0;
}
</style>
<script>document.write("<script type='text/html' id='text-html'>");</script>
</head>
<body>
<img src="http://placehold.it/600x400" alt="" width="600" height="400"/>
</body>
</html>
<!--</script>
<script>
var content = document.getElementById("text-html");
document.write(content.innerHTML);
content.parentNode.removeChild(content);
</script>-->
<template>element, but you’d probably be better off just escaping</script>.<script>tag. It’s a very special case. Anyways, what do you mean by “in some cases, it would actually be displayed”?