6 years ago I asked a question about rewriting text files displayed in a browser using greasemonkey. Can I Make Greasmonkey Scripts Run On Text Files?
I am now coming back to a similar problem and I tried to paste it in to Tampermonkey but it doesn't replace the text.
What am I doing wrong here?
// ==UserScript==
// @name Rewrite LLVM License
// @namespace http://tampermonkey.net/
// @version 0.1
// @match http://llvm.org/releases/2.8/*
// @include http://llvm.org/releases/2.8/LICENSE.TXT
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant none
// ==/UserScript==
(function() {
//Just to tell the linter that $ is defined in jquery
/* global $ */
//Browsers display text in a pre tag
var pageTextNd=$("body > pre");
//Replace the text LLVM
var newPageTxt=pageTextNd.text().replace("LLVM", "Ernst Blofeld");
//Rewrite the page
pageTextNd.text(newPageTxt);
})();
It looks like the page you're interested in redirects to:
https://releases.llvm.org/2.8/LICENSE.TXT
so that's what you need to set your @include
or @match
to.
You also want to replace all instances of LLVM, so use .replaceAll
:
// ==UserScript==
// @name Rewrite LLVM License
// @include https://releases.llvm.org/2.8/LICENSE.TXT
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant none
// ==/UserScript==
(function() {
//Just to tell the linter that $ is defined in jquery
/* global $ */
//Browsers display text in a pre tag
var pageTextNd=$("body > pre");
//Replace the text LLVM
var newPageTxt=pageTextNd.text().replaceAll("LLVM", "Ernst Blofeld");
//Rewrite the page
pageTextNd.text(newPageTxt);
})();
If you don't want to rely on replaceAll
, use a regular expression with .replace
instead: /LLVM/g
.
It seems quite strange to rely on jQuery for something this trivial though - you can very easily accomplish this without a library:
// ==UserScript==
// @name Rewrite LLVM License
// @include https://releases.llvm.org/2.8/LICENSE.TXT
// @grant none
// ==/UserScript==
const pre = document.querySelector('body > pre');
pre.textContent = pre.textContent.replaceAll('LLVM', "Ernst Blofeld");