Skip to content

Instantly share code, notes, and snippets.

@corentinbettiol
Last active April 20, 2023 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corentinbettiol/9cb4543c96ec94137bd0adb0452f1c34 to your computer and use it in GitHub Desktop.
Save corentinbettiol/9cb4543c96ec94137bd0adb0452f1c34 to your computer and use it in GitHub Desktop.
Python & Pip invoke commands as a *monkey extension

This extension just replaces python by python3 and pip by python3 -m pip.

Example

image

// ==UserScript==
// @name Python & Pip invoke commands
// @version 1
// @grant none
// @author Corentin Bettiol
// @description This extension just replaces `python3` by `python3` and `python3 -m pip` by `python3 -m pip`.
// @homepageURL https://gist.github.com/corentinbettiol/9cb4543c96ec94137bd0adb0452f1c34
// @match *://*/*
// @run-at document-end
// ==/UserScript==
// Thx https://stackoverflow.com/a/24419809/6813732
var replaceArry = [
[/(?<!_)python(?!3|_)/g, 'python3'],
[/(?<!python3 -m |_)pip(?!_)/g, 'python3 -m pip'],
];
var numTerms = replaceArry.length;
var txtWalker = document.createTreeWalker (
document.body,
NodeFilter.SHOW_TEXT,
{ acceptNode: function (node) {
//-- Skip whitespace-only nodes
if (node.nodeValue.trim() )
return NodeFilter.FILTER_ACCEPT;
return NodeFilter.FILTER_SKIP;
}
},
false
);
var txtNode = null;
while (txtNode = txtWalker.nextNode () ) {
var oldTxt = txtNode.nodeValue;
for (var J = 0; J < numTerms; J++) {
oldTxt = oldTxt.replace (replaceArry[J][0], replaceArry[J][1]);
}
txtNode.nodeValue = oldTxt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment