I don't think this exists, but it should. If eventually, you could hook into the posts as easy as this:
<?php
use Flarum\Extend;
use Flarum\Frontend\Document;
return [
(new Extend\Frontend('forum'))
->content(function (Document $document) {
$document->discussions[] = '<script>alert("Hello, world!")</script>';
})
];
Or, something similar. Basically, instead of adding scripts to the head and footer which you can already easily do, we need a way to easily do the same for each post like this one that you're reading right now. What would this do? It would allow one to extremely easily, be able to inject code which could be added to all posts. For example, I want to add this simple JavaScript to my Flarum to create hashtags:
<script>
/*
Select all paragraphs
Replace any hashtags with links.
*/
(function (win,doc) {
'use strict';
var siteURL = 'https://google.com',
entries = doc.querySelectorAll('p'),
i;
if ( entries.length > 0 ) {
for (i = 0; i < entries.length; i = i + 1) {
entries[i].innerHTML = entries[i].innerHTML.replace(/#(\S+)/g,'<a href="'+siteURL+'search/$1" title="Find more posts tagged with #$1">#$1</a>');
}
}
}(this, this.document));
</script>
It would be so amazing and magical, if I could just do:
<?php
use Flarum\Extend;
use Flarum\Frontend\Document;
return [
(new Extend\Frontend('forum'))
->content(function (Document $document) {
$document->discussions[] = '<script>
(function (win,doc) {
'use strict';
var siteURL = 'https://google.com',
entries = doc.querySelectorAll('p'),
i;
if ( entries.length > 0 ) {
for (i = 0; i < entries.length; i = i + 1) {
entries[i].innerHTML = entries[i].innerHTML.replace(/#(\S+)/g,'<a href="'+siteURL+'search/$1" title="Find more posts tagged with #$1">#$1</a>');
}
}
}(this, this.document));
</script>';
})
];