aclist / kbin-kes

Add-on manager and scripting framework for kbin
MIT License
24 stars 8 forks source link

How do I create my own script that loads something into each entry? #289

Closed look997 closed 3 months ago

look997 commented 5 months ago

I want to get a callback with each entry (the HTML element of the entry), and do something with it. In my case, read the Original URL and add an icon after the "more" button that loads a preview of the original post. I described it here as a proposal: https://codeberg.org/Kbin/kbin-core/issues/1380

If I make a script, I don't have to wait for kbin to add it itself.

aclist commented 5 months ago

There is an add-on that functions in a similar manner located under Threads > Expand post text in thread index. While it does not render the page exactly in the way you showed, it does fetch the entire body of the original post (as text).

It sounds like the main functionality you want is the ability to see likes, upvotes, etc.

To fetch remote content, you would have to use the genericXMLRequest() function provided by KES. The "expand posts" mod (/mods/expand_posts/expand_posts.user.js) would be a jumping-off point showing how to use this function to fetch content on demand.

Once you have the remote XML document, you can filter parts of it or embed it in the original DOM tree.

You'll need to iterate through each entry and embed a click event listener on some button or link. The majority of scripts have this sort of functionality and use document.querySelectorAll('element').forEach().

Lastly, since this must apply to new threads that get loaded on the page when infinite scroll is on, set the recurs key to true in the JSON file for the mod.