Skip to content

Instantly share code, notes, and snippets.

@thegitfather
Last active July 13, 2021 15:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thegitfather/679c9bc76e9b058790e3 to your computer and use it in GitHub Desktop.
Save thegitfather/679c9bc76e9b058790e3 to your computer and use it in GitHub Desktop.
load/replace css/js
// ==UserScript==
// @name load_replace_css_js
// @namespace https://gist.github.com/search?q=thegitfather+UserScript
// @author thegitfather
// @version 0.11
// @description load/replace css/js
// @updateURL https://gist.github.com/thegitfather/679c9bc76e9b058790e3/raw/load_replace_css_js.user.js
// @downloadURL https://gist.github.com/thegitfather/679c9bc76e9b058790e3/raw/load_replace_css_js.user.js
// @include http://127.0.0.1/*
// @grant none
// ==/UserScript==
function loadjscssfile(filename, filetype){
if (filetype=="js"){ //if filename is a external JavaScript file
var fileref=document.createElement('script');
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", filename);
}
else if (filetype=="css"){ //if filename is an external CSS file
var fileref=document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", filename);
}
if (typeof fileref!="undefined")
document.getElementsByTagName("head")[0].appendChild(fileref);
}
function createjscssfile(filename, filetype){
if (filetype=="js"){ //if filename is a external JavaScript file
var fileref=document.createElement('script');
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", filename);
}
else if (filetype=="css"){ //if filename is an external CSS file
var fileref=document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", filename);
}
return fileref;
}
function replacejscssfile(oldfilename, newfilename, filetype){
var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none"; //determine element type to create nodelist using
var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none"; //determine corresponding attribute to test for
var allsuspects=document.getElementsByTagName(targetelement);
for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!==null && allsuspects[i].getAttribute(targetattr).indexOf(oldfilename)!=-1){
var newelement=createjscssfile(newfilename, filetype);
allsuspects[i].parentNode.replaceChild(newelement, allsuspects[i]);
}
}
}
// usage examples:
//loadjscssfile("http://localhost/myproject/script.js", "js");
//loadjscssfile("http://localhost/myproject/styles.css", "css");
//replacejscssfile("/frontend/default/js/script.js", "http://localhost/myproject/script.js", "js");
//replacejscssfile("/frontend/default/js/styles.css", "http://localhost/myproject/styles.css", "css");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment