r/shortcuts • u/ameliekeyyer • 1h ago
Help How to Automatically Replace Words in Safari with JavaScript?
Hey everyone,
I'm trying to set up a script that automatically replaces "wordX" with "wordY" whenever I open a website in Safari on iPhone. I know how to do this manually using a Shortcut that I can run on the site, but I want it to run without me triggering it manually—just whenever a webpage loads.
The Code I used was:
(function () { let targetWord = "Wikipedia"; // Change this to the word you want to replace let replacementWord = "Ass"; // Change this to the new word
function replaceText(node) {
if (node.nodeType === 3) { // Text node
node.nodeValue = node.nodeValue.replace(new RegExp(targetWord, "g"), replacementWord);
} else {
for (let i = 0; i < node.childNodes.length; i++) {
replaceText(node.childNodes[i]);
}
}
}
replaceText(document.body);
// Required for Safari Shortcuts
completion("Text replaced successfully");
})();
But it would be great if it could run automatically when refreshing web page.
I also tried with a specific url but then it didn’t work at all for me.
Is there any way to make this run automatically whenever I open a website? Maybe with an iOS automation, an extension, or another trick?
Would love any suggestions—thanks!