« first day (1 day earlier)      last day (25 days later) » 

10:39 AM
@MartinR: Thanks for that. I'm also not a JS expert; I can generally figure out how to get something to work, like you, but the fine details of javascript and W3C specifications are notoriously inscrutable. From your comments it seems that TM or your browser does not like unscoped variables, which explains why it only complains about "host" and "menu" but not about "link".
As a side effect, learning about that made me realize why an earlier code I had was failing, because the variable I used in a closure was being modified by the for-loop.
The earlier code was to make the eventListener only update the links inside the js-post-menu that you click on. But since each page on SE has so few such links, updating all would be so quick anyway.
I think the standard way of chucking everything into a scope is to wrap the whole thin in an anonymous function like:
// ==UserScript==
// @name        CDStatus
// @namespace   SE
// @include     *
// @version     1
// @grant       none
// ==/UserScript==

function(){

if( location.hostname.match(/(^|\.)(stackexchange|(stack|math)overflow|superuser|askubuntu|serverfault)\.(com|net)$/) )
{
	function cdstatus()
	{
		for( link of document.getElementsByClassName("js-close-question-link") )
		{
			var title=link.getAttribute("title");
			if( title.match(/voted|other users/) ) link.innerHTML=link.innerHTML.replace(/Close[ \n]/,"Close-voted ");
 
@user21820: I also noticed that your script is injected in every page. – github.com/SpectricSO/stack-scripts/tree/main/scripts/… is another user script I am currently using (see also stackapps.com/q/9074/74164). It uses more specific @match directives.
 
@MartinR You're right; ideally I ought to use the match directive. The reason for the lapse was that this was just a piece of code from a longer script that I do inject into every page to make things look nicer, not limited to SE haha..
 
10:55 AM
@user21820 It does complain about “link” and “menu”, I fixed that with for(let link ...) and for(let menu ...)
I also had to use the “block scoped” let title = ... to avoid errors about duplicate declaration.
 
 
1 hour later…
12:06 PM
@MartinR Oh that's interesting; I suppose my browser is very permissive.
Though I thought JS "var title" inside a for-loop would limit the scope of "title" to that loop.
I'm not sure why your browser doesn't agree.
 
let defines a block scoped variable and var a function scoped variable. So, as I understand it, you cannot have two var title variables inside the same function.
 
 
1 hour later…
1:17 PM
@MartinR Oh ok so my browser is just letting me get away with bad syntax. Thanks!
 
 
2 hours later…
3:27 PM
@MartinR Why does this post say "In strict mode, var will let you re-declare the same variable in the same scope"?
 
4:23 PM
Wow JS is really disgusting:
4
A: let keyword in the for loop

Gerard ONeillIMHO -- the programmers who first implemented this LET (producing your initial version's results) did it correctly with respect to sanity; they may not have glanced at the spec during that implementation. It makes more sense that a single variable is being used, but scoped to the for loop. Espe...

I can't believe the implementation of "let" differs between old and new browsers... I learnt JS way before "let" even appeared, so I didn't know its horrendous history.
 
I just realized that that are not JavaScript errors, but errors from ESLint (eslint.org/docs/user-guide/configuring) which seems to be active by default in Tampermonkey.
 
@MartinR Ah I see.
 
 
1 hour later…
5:51 PM
// ==UserScript==
// @name        CDStatus
// @namespace   SE
// @match		*://*.mathoverflow.net/*
// @match		*://*.stackoverflow.com/*
// @match		*://*.stackexchange.com/*
// @match		*://*.superuser.com/*
// @match		*://*.askubuntu.com/*
// @match		*://*.serverfault.com/*
// @match		*://*.stackapps.com/*
// @version     1
// @grant       none
// ==/UserScript==

(function(){
	function cstatus(link)
	{
		return function()
		{
			let title=link.getAttribute("title");
			if( title.match(/voted|other users/) ) link.innerHTML=link.innerHTML.replace(/Close[ \n]/,"Close-voted ");
@MartinR @amWhy: Updated the script to instantly update upon voting. Previously it used a sloppy method that sometimes fails when the browser lags.
 
Thanks, @user21820 I plan to devote a block of time to updating all your helpful info!!
 
This script requires GreaseMonkey (for Firefox derivatives) or TamperMonkey (for Chrome). After installing GM/TM, save the script in a file with extension ".user.js" (e.g. "cdstatus.user.js") and open it in the browser (or drag it in).
If the script name has changed, go to your browser's Addons page to manage userscripts and remove the old one.
 
 
1 hour later…
7:14 PM
@user21820 Looks good! No errors or warnings in my Safari browser with Tampermonkey!
 
@MartinR And does it work with immediate update effect? It's the first time I used MutationObserver!
 
Yes, it works perfectly so far!
 
@MartinR Thanks for your feedback!
 

« first day (1 day earlier)      last day (25 days later) »