« first day (766 days earlier)      last day (671 days later) » 

2:53 PM
I cannot figure this out. Does anyone know where the Follow button becomes Following in StackExchange? If you request a page that contains a post you're following the HTML ships in with <button ...> Follow </button> and then something changes it to "Following" but I can't seem to find it.
It kinda looks like StackExchange.vote.follow_init does something, but it looks for aria-pressed but that value doesn't come with the HTML. I'm so confused
<div class="flex--item">
    <button type="button"
            id="btnFollowPost-59772497"
            class="s-btn s-btn__link js-follow-post js-follow-answer js-gps-track"
            data-gps-track="post.click({ item: 14, priv: 17, post_type: 2 })"
            data-controller="s-tooltip "
            data-s-tooltip-placement="bottom"
            data-s-popover-placement="bottom"
            aria-controls=""
            title="Follow this answer to receive notifications">
        Follow
    </button>
That's what comes in (yes that's a post I'm already following) so something is changing it on the client side, but I can't seem to figure out what that is.
 
oh, yeah, that. You might want to examine my following-overcharged recent script where I had to deal with that.
in the end, I decided to simply compare the normalized text of the button
 
3:10 PM
I'm trying to add a follow button to a page it does not already exist (NATO) and I'm trying to figure out how to determine if the post is already a post I am following and changing the Follow button to Following in that case.
I know it's happening on the client side because there's no indication (that I can find) that the information comes from the server. I just can't figure out where it happens...
 
@HenryEcker If you haven't already found it, here is a great tip that Oleg taught me: here is the unminified source code. You add -dev to the cdn to get cdn-dev and you get the resource. In there follow_init() (line 15491) calls follow() (line 15936) which in turn calls updateFollowUI() (line 15920). It's there that the text is changed.
 
I was taught it myself :) Was it Makyen? I think it was them
 
@VLAZ Thank you. Yes I've found that. Unfortunately, follow_init just binds the click handlers and modifies values when you click. There's something that goes through before that which determines if you are already following the post and updates the values without you clicking the button. That's I what I can't find... =(
 
@HenryEcker Ah, sorry. I see what you need now. I specifically followed the click handler
 
It's okay. I do appreciate the link. That's super helpful when trying to figure out how to do things.
But yes, I already have the follow buttons working, and you can follow posts from NATO, I just need to figure out how to turn Follow into Following if you're already following the post.
 
3:21 PM
There is StackExchange.vote.highlightExistingVotes() which also calls updateFollowUI(), however, it expects to be given list of votes. Let me track down where that comes from.
 
Yeah, that looks like what I'm looking for. election_init seems to feed that function but where do the votes come from... hm
Looks like all the way back at question.fullInit
        StackExchange.question.fullInit = function (options) {
            StackExchange.vote.init(options.votesCast);
Okay. So the list of votes comes SSRed over in the head...
StackExchange.question.init({canOpenBounty:true,votesCast:[{"PostId":72773479,"VoteTypeId":21}],canViewVoteCounts:true,showTrendingSortLaunchPopover:true,showTrendingSortPostLaunchPopover:false,totalCommentCount:0,shownCommentCount:0,enableTables:true,questionId:59772112});
 
Just found that, as well.
This...is stupid.
 
Well... That's unfortunate indeed.
 
If that code snippet is present on the NATO page, you can try to find it and eval it. It's ugly but it would do what you want.
If not, then...I don't know how you'd check if you've voted for a post. If there is some way to do that with an HTTP call, you can then just call StackExchange.question.init with the data. even if you need to massage it into the correct shape.
If that fails, you could do it even uglier by scraping the original page and trying to find that initialisation call.
 
Unfortunately not even that's an option. There's no question on that page so there's no question.init...
Yeah. I'm going to see if there's an end point I can play with, but I don't want to get rate limited scraping all of those for every post on the page...
 
3:35 PM
You could also post a feature request for SE to add the follow button on that page. And then wait 6-8
 
I don't really even want the functionality on that page. It's just inline editing is enabled on the page. And the response from the editor gives me the full UI back so it adds the missing buttons (and the editor usercard) to the DOM.
I didn't like the way it looked just having those buttons appear there.
 
Workaround - remove the Follow button before inserting in the page.
 
This is what the rework currently looks like.
 
Or...just have CSS that matches it and sets display: none
 
Yeah. That's probably what I'm going to end up doing given all the fuss =/
 
3:39 PM
You can do the time-honoured //TODO fix Follow button later
 
I really would like it if they would just ship the answers in the Stacks answer component natively. Idk why it's just a list of answers with 0 controls. For a moderation tool it's not more effect than the search box...
 
Almost like almost no effort has been put into making moderation easier.
 
@HenryEcker you can make a GET request to <origin>/posts/<postId>/votes
in return, it will send an array of votes, and if the post is followed, it will contain {"PostId":418953,"VoteTypeId":21}
does that work for you?
 
3:54 PM
@OlegValteriswithUkraine It seems like exactly the format the init() method consumes
 
@VLAZ likely the endpoint returns the same data as SSR'd for the init method, yeah
 
@OlegValteriswithUkraine Oh cool. Uhhhh still need to figure out the rate limit thing though.
 
Probably. I'm just not at all well-versed into the API SE exposes.
 
@HenryEcker I don't think it's rate limit is documented, but I'd still wait a couple hundred of milliseconds just in case
 
Oh wait! That means I can also add a voting component too. That should give me back up/down votes too.
 
3:58 PM
@HenryEcker I thin you should be fine. This is calling the site itself, not the SE API. There is some sort of limit but I'm not sure what. I've hit it a couple of times. One was when I just opened a bunch of links at once, another when I was trying to audit my reputation history and was going through each page, opening all collapsed events, and each of those was making a call.
 
@HenryEcker It does. Nice
So then follow and a voting UI are on the todo list
 
YW :) sorry for not remembering it earlier
 
//TODO fix Follow button
//TODO add voting buttons
 
@VLAZ /vote/<id> is pretty resilient to being hammered into in my experience, /votes should be so, I think. Confirmed by accidentally sending thousands of requests to follow a post - it started to catch up after a couple dozen reqs or so :) Still, won't hurt to throttle for a reasonable amount of time
 
When I was hit with the limit, I did do a somewhat unreasonable amount of requests. Not very "normal user". I opened maybe 50-60 tabs if not more and each of those would also be firing a bunch of extra requests like the Roomba forecaster. And when I was auditing my rep history, I was opening all collapsed entries at once with a little JS. So, I'd go to a page, fire maybe 10-20 requests, go to the next one do the same, etc.
So, the limit is low as I was able to triger it basically by browsing the site. Even if slightly abnormally. But I don't think the NATO page would be that much of a burden. Unless you sit there and refresh it multiple times. And even then, you'd get timed out for 10-ish minutes, IIRC.
 
4:07 PM
honestly, I am starting to think someone should create a shared request throttler for userscripts [looks at the backlog, sighs deeply]
 
Probably not worth it. Every single userscript has to opt in for it. And that's not going to happen.
 
at least it can be provided as a peer dependency, I think - been on my mind for a while
 
4:27 PM
I went with 75 ms and will adjust if needed. But following is now working as expected. Thank you both :) Now just need to do the voting component (though I might should hold off a bit given the current redesign test thing)
 
NP! It seems like they just reused the new component and only changed how voting buttons look - my scripts that rely on them are still functioning despite the A/B test, so the underlying logic seems to be the same
 
Yeah. The HTML is actually the harder part, I didn't want to go through and JQuerify the voting component into the script for it to change.
Oh wait... I forgot the other part, I still don't know how to get the current score of a post... Welp
Maybe that'll be a tomorrow project then
 
4:53 PM
@HenryEcker I got you covered, here are some generic buttons you can use instead: đź‘Ť and đź‘Ž :P
@HenryEcker You can use the StackExchange API /answers/{ids} for that.
To get all the answer IDs from the page, you can use Array.from(document.querySelectorAll("[data-answerid]")).map(el => el.dataset["answerid"]).join(";")
Or slightly more succinct: Array.from(document.querySelectorAll("[data-answerid]"), el => el.dataset["answerid"]).join(";") (dropping the .map() call since Array.from can do the same if you pass a second argument to it).
 
 
5 hours later…
9:50 PM
@VLAZ I was trying to avoid the API like the plague on this one. Just to see if I could do it (also to avoid the overhead)
 
I'm not sure how else to get the scores. The site API might have something but I'm not at all familiar with it.
 
Yeah that's okay. I think I'll just probably just hide it.
 
10:46 PM
@HenryEcker there's yet another endpoint for doing so :)
<origin>/posts/<postId/vote-counts
 
Idk. Maybe I just suck it up and use the API. I don’t want to be sending 40 requests per page. =(
 
gets the breakdown directly: {up: "+53", down: "-26"}
 
I was really trying to avoid requiring the user to authenticate
 
nope, no auth :)
 
@OlegValteriswithUkraine do you need 1k to hit that end point.
Also does that have the 1s fetch vote counts rate limit?
 
10:49 PM
@HenryEcker not sure if you really have to - but it does need you to be at least logged in
@HenryEcker yea, it has
which might be an issue
 
Greeeaaat. Okay then there’s 20 NATO per page so that’s really sub optimal.
 
guess so, hmmm
 
I might suck it up and have people authenticate with the API. Then bulk pull score and votes (like up/down following) in 1 request per page…
Anyone have a boiler plate authenticate client side and save the key and token with GM_setValue?
 
not at the ready, sorry - but the docs have a working example boilerplate that might help (apart from saving the token, but I am sure it's easy to do for you)
speaking of the API... wouldn't it be enough to just get the score?
if you don't need the breakdown immediately (and you might want to fetch the endpoint mentioned above on click just as SE does), you can get away with 1 request to the API and only the registered API key, score is public
that's not ideal, but if batching ids, it's probably the best possible outcome + it will be in line with how the rest of the site works (speaking of vote breakdowns) + no need to force users to authenticate
 
11:34 PM
@OlegValteriswithUkraine Yeah. Idk though because if I'm going to make an API request I might as well just make 1 api request.... Why make one public API request and then 20 separate /votes requests?
 
eh, can one even get the follow status from the API? I don't recall it being present on any of the interfaces - am I missing something?
 
Hmmmm I guess you're right. It's just upvoted and downvoted. For some reason I was thinking /posts with a filter would return all votes....
 
yeah, I don't remember anything in the API docs that would allow to fetch the follow status for a given user - I also recently investigated it in depth when creating following-overcharged for basically the same reason as you and came up with nothing
 
Idk... I wish the API were either more comprehensive or the end-points outside of the API were better documented. I do not want to make 20 requests to get follow status. I would absolutely make 1 request if the system supported it. =/
This is, of course, ignoring the fact that NATO is, by default, a <table> with overflowing elements and no moderation actions permitted from that tool
 
so would I... The API sorely lacks a lot of things, actually - but apparently it's on the same level of priority as the NATO page itself
I don't even think we are supposed to fetch those undocumented endpoints from SE's POV too, so at some point I am sure we'll get a lot of useful userscripts broken
but it's either that or not doing anything at all, so...
besides, a lot of endpoints are just ... not very useful on SO as they can't be effectively used on our scale with the limitation of 100 posts per page
 

« first day (766 days earlier)      last day (671 days later) »