Simplify script injection to single script with data attribute configuration
- Replace dual script injection (external + inline) with single script approach - Pass site configuration via data attributes instead of inline JavaScript - Update library auto-initialization to read config from script data attributes - Reduce HTML bloat by eliminating 30+ lines of inline initialization code - Maintain future-proof CDN compatibility and duplication prevention Enhanced files now contain only one clean script tag with configuration.
This commit is contained in:
@@ -364,42 +364,8 @@ func (i *Injector) InjectEditorScript(doc *html.Node) {
|
||||
return
|
||||
}
|
||||
|
||||
// Create script element that loads insertr.js from our server
|
||||
scriptHTML := fmt.Sprintf(`<script src="http://localhost:8080/insertr.js" data-insertr-injected="true"></script>
|
||||
<script type="text/javascript" data-insertr-injected="true">
|
||||
// Initialize insertr for demo sites
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
if (typeof window.Insertr !== 'undefined') {
|
||||
console.log('✅ Insertr library loaded successfully');
|
||||
|
||||
// The library has auto-initialization, but we can force initialization
|
||||
// with our demo configuration
|
||||
window.Insertr.init({
|
||||
siteId: '%s',
|
||||
apiEndpoint: 'http://localhost:8080/api/content',
|
||||
mockAuth: true, // Use mock authentication for demos
|
||||
debug: true
|
||||
});
|
||||
|
||||
console.log('✅ Insertr initialized for demo site with config:', {
|
||||
siteId: '%s',
|
||||
apiEndpoint: 'http://localhost:8080/api/content',
|
||||
mockAuth: true
|
||||
});
|
||||
} else {
|
||||
console.error('❌ Insertr library failed to load');
|
||||
|
||||
// Fallback for demo gates if library fails
|
||||
const gates = document.querySelectorAll('.insertr-gate');
|
||||
gates.forEach(gate => {
|
||||
gate.addEventListener('click', function(e) {
|
||||
e.preventDefault();
|
||||
alert('🚧 Insertr library not loaded\\n\\nPlease run "just build-lib" to build the library first.');
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
</script>`, i.siteID, i.siteID)
|
||||
// Create script element that loads insertr.js from our server with site configuration
|
||||
scriptHTML := fmt.Sprintf(`<script src="http://localhost:8080/insertr.js" data-insertr-injected="true" data-site-id="%s" data-api-endpoint="http://localhost:8080/api/content" data-mock-auth="true" data-debug="true"></script>`, i.siteID)
|
||||
|
||||
// Parse and inject the script
|
||||
scriptDoc, err := html.Parse(strings.NewReader(scriptHTML))
|
||||
@@ -414,7 +380,7 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("✅ Insertr.js library and initialization script injected")
|
||||
log.Printf("✅ Insertr.js library injected with site configuration")
|
||||
}
|
||||
|
||||
// injectAllScriptElements finds and injects all script elements from parsed HTML
|
||||
|
||||
Reference in New Issue
Block a user