Jump to content

User:Jdlrobson/User scripts with client errors

From Meta, a Wikimedia project coordination wiki

You can help fix many of the JavaScript errors on Wikipedia's many projects! This wiki page accompanies my article Diving into Wikipedia's ocean of errors. A graph of all errors to our software is provided on grafana and publicly accessible.

You can help me by fixing the associated gadgets, or sharing this page with maintainers who might be able to fix the errors.

Many editors use scripts and gadgets which throw JavaScript errors. Now that we track these errors, we should address these issues to make these tools more reliable and to protect the privacy of these users.

For those with access to Logstash please view the the logstash dashboard to identify bugs and using the stack trace and file_uri locate and fix the most regularly occuring editors.

If you do not have access to Logstash, but are able to edit user scripts you can also help! I hope to regularly update this page with links to scripts with problems. I will list the script causing the error, the error being thrown and the frequency at which it occurs. Please feel free to edit this page to mark any bugs you have fixed.

My hope is to regularly update this page with anonymized data.

Current Errors you can help with

[edit]

SyntaxError: Invalid regular expression: invalid group specifier name

[edit]

Latest edited 11th July 2022.

There is a bug in popular gadgets. This occurs on versions of Safari which do not support look behind (Safari 14 or Safari 15 desktop and Safari on many iPads). To address this bug we would need to check for support before attempting to load and execute the JavaScript or revise the regular expressions to not use look behind.

The bug is present in the following scripts


@Novem Linguae: @Ingenuity:@Bradv:@Cobaltcigs:@Qwerfjk:@X0stark69:

This accounts for 11,540 of all our errors every week (about 5% of all errors). Your help fixing this issue would be much appreciated

Errors relating to T72470

[edit]

Over a 2000 errors a day pollute production logs, making it harder for us to identify errors. If you have been pinged here, you maintain a script is broken, and throwing errors in such a way that undermines your privacy. It need updating. If the script is unmaintained or no longer needed, please fix this problem by blanking the page. To fix the update is simple:

  • Search for "wg"
  • Wrap any words prefixed with wg like so: wgTitle => mw.config.get('wgTitle')
  • For ReferenceError: skin is not defined, replace skin with mw.config.get('skin')

Last updated 2nd July 2021

Why fix?

[edit]
  • Help make non gadget errors easier to uncover and help us build better software
  • More stable scripts and gadget for you
  • Fewer pings from this page.
  • Protect your privacy. If you are throwing an error, and you are the only person using a gadget or script, you are leaking information about your viewing habits to those trusted with this data.
  • Fixing errors helps us identify and allow other users to fix errors in their own scripts!

What if my code is unmaintained?

[edit]

If you are not maintaining the code, it is in the best interest of everyone, that you blank the page and/or get the page deleted. If users want to continue to use and maintain your script they can restore it themselves in their own user namespace and it will be easier to drive changes, particularly if the script is a user script.

How to fix?

[edit]

Uncaught SyntaxError

[edit]

Someone/something/possibly you is possibly loading a script as CSS instead of JS. If it's not you, you may be able to find out who using https://global-search.toolforge.org/? or asking users using the script using your local community portal. Usually this means the user is using importScript instead of importStylesheet or passing ctype=text/javascript to mw.loader.using rather than ctype=text/css

ReferenceErrors

[edit]

You are using a variable that may or may not exist. The script likely doesn't work. This may be helped by prefixing `window.` to signal you are accessing a global variable or taking a defensive programming approach to check its type. Clearing the wiki page may be a solution.

In the case of wg prefixed variables please use mw.config e.g. mw.config.get('wgPageName') rather than wgPageName.

Alternatively, review ResourceLoader/Migration_guide_(users) for migration guidelines.

As a last resort, use typeof to check if the variable exists e.g.

// Code that will work 
if (typeof mwCustomEditButtons !== 'undefined') {
 // run code
 window.mwCustomEditButtons.push('btn')
}}

// Note the following will not work:
if (mwCustomEditButtons) {
 // run code
}}

TypeError

[edit]

If you have an error such as TypeError: document.querySelector(...) is null it means you are trying to query something that doesn't exist - for example the first item in an empty array. This can happen when you use querySelector as that may return null.

// bad
var x = document.querySelector('.element').getElementsByTagName('li');

// good
var el = document.querySelector('.element');
if (el) {
    var x = el.getElementsByTagName('li');
}

Error: AJAX error: error / custom errors

[edit]

You likely have a script that itself throws errors. eg.

throw new Error("my script error")

If you want to deal with this error please get a logstash account to further debug this. If the purpose of the code is to alert yourself to errors, please use mw.log.warn or mw.log.error - this will ensure a warning appears in your JavaScript console but does not add noise to those using logstash.

Script errors

[edit]

These errors occur when your script is being loaded by another project. e.g. A script on commons is being loaded on French Wikipedia. At the current time we are unable to know the error that's been thrown or the stack trace (see https://raygun.com/blog/script-error/ for more information), but if there are more than 2 errors for your script, it is likely script error is being caused by the identifiable error. If it is the only error, it's possible you might be able to uncover the stack trace and error using your developer console and manual testing by loading on those wikis.

In the case of a global user script on meta.wikimedia, although your script runs on meta.wikimedia.org it is incompatible with one of the wikis you frequent. If you don't understand the code you are running please consider blanking the page with the code.