ccd0 / 4chan-x

Adds various features to anonymous imageboards.
https://www.4chan-x.net/
Other
987 stars 135 forks source link

4chanXInitFinished fails to fire when connection is throttled #1635

Open ghost opened 6 years ago

ghost commented 6 years ago

I generally use the 4chanXInitFinished event to wait for 4chanX content to load and generally this works fine but today for whatever reason I experienced really bad connection speeds (400 kbps in download) and noticed that multiple functions of my userscript failed probably due to the amount of data to download that becomes significant with really low speeds. I noticed that the script struggles to run when I'm visiting the index (in my case I'm in catalog mode) but it fires fine 9 times to 10 when I'm in a thread where the amount of data to load is less. Generally to counterbalance the effects of slow speeds I wait X amount of ms and re-run my functions hoping for the best but I think this is not really a fine solution that applies to all the cases.

Is there an event that actually fires when all of the nodes generated by 4chan X have been processed? Since parts of my userscript depends on nodes generated only by 4chan X, I'm looking for an event that assures me that all of them have been added in the document and that I can run my function without errors.

For now I found out that using IndexRefresh event with really slow connections let me wait for 4chan X nodes to generate. This happens probably because the amount of data used by 4chan X nodes is less than the whole index content so being smaller it loads faster and this assures me that when index content is loaded, 4chan X nodes are loaded too but this again feels like some kind of hack just like waiting for X amount of ms and running a function hoping that the nodes have been finally processed.

Do you think your custom events can help me in this case? If not, do you plan to add an event that fires when all the nodes generated by 4chan X have been loaded in the page?

ccd0 commented 6 years ago

What nodes are you waiting for? Nodes inside posts or something else?

When JSON Index is on, 4chan ignores the original posts in the index and replaces them with posts generated from JSON. 4chanXInitFinished is not applicable here, and you will have to use IndexRefresh or PostsInserted to wait for the posts. 4chan X inserts nodes into these posts before inserting them into the index. More posts get generated and inserted whenever you change the page or refresh the index.

ghost commented 6 years ago

I have to wait for the QR node, the thread-watcher node and the header node. All the rest is managed directly by 4chan so I don't have to wait anything. If you say I can use IndexRefresh or PostInserted without worries I'll use this method, I just wanted to check if something ad-hoc was in program. I thought that an event that fires when 4chanX nodes are generated might be useful for those one-time edits someone would want to do to 4chanX nodes.

By the way, is the IndexRefresh event fireable even when I'm in threads? My functions make one-time changes adding some static elements in your nodes so it's not like I need to re-run them whenever the index is refreshed or when a new post is inserted

edit: now that I remember correctly there is an event in the API that waits for QR creation so I guess I don't need an event for that node, but the issue for the other nodes remain

saxamaphone69 commented 6 years ago

I've run into this problem in the past too. An alternative is you could also use Mutation Observers if you wanted to watch the document and wait for certain elements to be added, and then make the changes (and also remove the observer once you've made the change).

It mightn't help, but I currently use this small ready script to wait for quote previews to be loaded so it can dynamically resize them in the event they are taller than the browser window, and also at one point when I converted the quick reply to use floating labels for aesthetic purposes.

In previous versions of my userscripts, if a thread was particularly long it would miss the 4chanXInitFinished call (in that 4chan X would finish "loading" before all the posts) and I wouldn't end up with my adjustments. I've found IndexRefresh works in the most part (I turn the post numbers in post summaries bold) but navigating using the back button doesn't trigger this event, and I also believe doing a search sometimes fails to fire too. Here's a list of the functions available fwiw:

events

ccd0 commented 6 years ago

I have to wait for the QR node, the thread-watcher node and the header node.

4chanXInitFinished / IndexRefresh / PostInserted are related to the posts, so they won't help with this, at least not as they are right now. There's an event for the Quick Reply, QRDialogCreation, but you'll probably need to use MutationObserver for the others.

ghost commented 6 years ago

Guess I'll have a look at the MutuationObserver then, thanks for help so far. What about the IndexRefresh and PostInserted events? Does the IndexRefresh fire when I open a thread (when I'm not in the index)?

ccd0 commented 6 years ago

IndexRefresh only happens on the index. It wasn't originally part of the documented API, but Name Sync started using it for detecting when new posts were added to the index so that it could add names to them, so I changed it so it worked for that purpose.