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
|
||||
|
||||
@@ -79,7 +79,18 @@ window.Insertr = {
|
||||
// Auto-initialize in development mode with proper DOM ready handling
|
||||
function autoInitialize() {
|
||||
if (document.querySelector('.insertr')) {
|
||||
window.Insertr.init();
|
||||
// Check for configuration from script data attributes
|
||||
const insertrScript = document.querySelector('script[data-insertr-injected]');
|
||||
const config = {};
|
||||
|
||||
if (insertrScript) {
|
||||
config.siteId = insertrScript.getAttribute('data-site-id') || 'demo';
|
||||
config.apiEndpoint = insertrScript.getAttribute('data-api-endpoint') || '/api/content';
|
||||
config.mockAuth = insertrScript.getAttribute('data-mock-auth') === 'true';
|
||||
config.debug = insertrScript.getAttribute('data-debug') === 'true';
|
||||
}
|
||||
|
||||
window.Insertr.init(config);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4104,7 +4104,18 @@ Please report this to https://github.com/markedjs/marked.`,e){let r="<p>An error
|
||||
// Auto-initialize in development mode with proper DOM ready handling
|
||||
function autoInitialize() {
|
||||
if (document.querySelector('.insertr')) {
|
||||
window.Insertr.init();
|
||||
// Check for configuration from script data attributes
|
||||
const insertrScript = document.querySelector('script[data-insertr-injected]');
|
||||
const config = {};
|
||||
|
||||
if (insertrScript) {
|
||||
config.siteId = insertrScript.getAttribute('data-site-id') || 'demo';
|
||||
config.apiEndpoint = insertrScript.getAttribute('data-api-endpoint') || '/api/content';
|
||||
config.mockAuth = insertrScript.getAttribute('data-mock-auth') === 'true';
|
||||
config.debug = insertrScript.getAttribute('data-debug') === 'true';
|
||||
}
|
||||
|
||||
window.Insertr.init(config);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user