Userscripts are a little secret that lets you run scripts on specific web pages to do your bidding and make your life easier - fun images are from from 'Rick and Morty' - Userscripts turn you into a Rick (a super genius).
Upgrading Old WebGPU and WGSL Scripts (Syntax Fixes)
WebGPU is new (very new) and as you'd would expect it has gone through a few updates to the language since it was released to the public - nothing major - just some tweaks and refinements to the syntax (both the API and the WGSL shaders).
As I've written hundreds of scripts - I wanted a quick way to upgrade any outdated versions - without having to manually go through them and fix the lines. The fixes aren't major - but it's time consuming if you've got lots and lots of scripts.
So this article gives a userscript with a bit of regular expression search/replace - to fix any WebGPU scripts on a page that are located in a textarea block.
The script isn't perfect - but it works with most of my examples - and you can easily take it further if you want. I didn't want to write a full language tokenizer/parser - just look for common language changes (e.g.
[[blocks]]
is no longer used). Search for these old styles and upgrade them.
Key points:
• Userscript uses the TamperMonkey browser extension
• Hardcoded regular expressions
• Timeout startup (waits 2 seconds before launching on new pages)
• Adds fixed button to the page (top left)
• Only replace scripts in 'textarea' (does not give any warnings or options)
Complete Code
// ==UserScript== // @name WebGPU and WGSL Upgrader Fixer Userscript // @namespace http://tampermonkey.net/ // @version 2024-06-24 // @description WebGPU and WGSL Syntax and Function Upgrade Fixer Early Releases // @author You // @match https://*/* // @icon https://www.google.com/s2/favicons?sz=64&domain=xbdev.net // @grant none // ==/UserScript==
function updatewebgpuscript(txt) { let newpresformat2 = ` //const presentationFormat = context.getPreferredFormat(adapter); - no longer supported const presentationFormat = navigator.gpu.getPreferredCanvasFormat(); `;
let newpresformat = `navigator.gpu.getPreferredCanvasFormat(); // context.getPreferredFormat(adapter); - no longer supported`;
// Restore struct blocks with added wrapping for (let i=0; i<array.length; i++) { let arr = array[i][0].replaceAll(';', ','); txt = txt.replace(/(\{STRUCT\})/, arr ); }// end for i
function setupWebGPUFix() { //(function() { 'use strict';
console.log('hell0'); // Function to replace text in all textareas function replaceText() { console.log('replaceText called'); // Find all textarea elements const textareas = document.querySelectorAll('textarea'); if (textareas.length > 0) { textareas.forEach(textarea => { console.log('text area...'); // Replace occurrences of 'catslikemilk' with 'nomilk' //textarea.value = textarea.value.replace(/catslikemilk/g, 'nomilk');
// Add click event to button button.addEventListener('click', replaceText);
// Append button to body document.body.appendChild(button); //})(); }
setTimeout(setupWebGPUFix, 2000);
Things to Try
The example shows a quick and dirty prototype to make my life easier - and can be used on examples you stumble across on the web if you want to do a quick update. Few ideas if you want to develop the prototype further:
• Extract the script and put into a popup dialog - highlight the lines that are going to be changed
• Include an 'option' dialog to select which language features to upgrade
• Extract the script, upgrade it, and put it into a new textarea (does not replace the original version)
• Split the text into API (JS) and the WGSL components - run the JS through Lint checker, and the WGSL through the WebGPU API compiler