145 lines
2.5 KiB
JavaScript
145 lines
2.5 KiB
JavaScript
/*
|
|
|
|
Copyright (c) 2020, 2023, The Unified Company.
|
|
|
|
This code is part of Unify.
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the ESA Software Community License - Strong Copyleft LICENSE,
|
|
as published by the ESA.
|
|
See the ESA Software Community License - Strong Copyleft LICENSE, for more details.
|
|
|
|
https://unifyjs.org
|
|
|
|
*/
|
|
|
|
|
|
export default class cacheDisabler{
|
|
|
|
randomString = String( Math.floor(Math.random() * 1000 )).padStart(4, '0');
|
|
|
|
trimParts( parts ) {
|
|
|
|
return parts.filter( function( entry ) { return entry.trim() != ''; });
|
|
|
|
}
|
|
|
|
removePreviousRandomNumber( parts, overrideSplit ) {
|
|
|
|
var overrideSplit = parts[3].split("?");
|
|
|
|
|
|
|
|
if( overrideSplit[1] ) {
|
|
|
|
// ';
|
|
if( parts[3].includes(";") ) {
|
|
|
|
var lastPart = overrideSplit[1].slice( overrideSplit[1].length-2, overrideSplit[1].length );
|
|
|
|
|
|
// '
|
|
} else {
|
|
|
|
var lastPart = overrideSplit[1].slice( overrideSplit[1].length-1, overrideSplit[1].length );
|
|
|
|
}
|
|
|
|
|
|
overrideSplit[1] = lastPart;
|
|
|
|
parts[3] = overrideSplit.join("")
|
|
|
|
|
|
}
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
injectWithoutSemicolon( parts, index, random, part ) {
|
|
|
|
var l = part.length;
|
|
|
|
parts[ index ] = part.slice( 0, l - 1 ) + random + part.slice( l - 1, l );
|
|
|
|
}
|
|
|
|
injectWithSemicolon( parts, index, random, part ) {
|
|
|
|
var l = part.length;
|
|
|
|
parts[ index ] = part.slice( 0, l - 2 ) + random + part.slice( l - 2, l );
|
|
|
|
}
|
|
|
|
injectRandomNumber( parts ) {
|
|
|
|
var randomString = "?disableCache=" + this.randomString;
|
|
|
|
var urlPartIndex = parts.length - 1;
|
|
|
|
var urlPart = parts[ urlPartIndex ];
|
|
|
|
if( urlPart.includes(";") ) {
|
|
|
|
this.injectWithSemicolon( parts, urlPartIndex, randomString, urlPart );
|
|
|
|
} else {
|
|
|
|
this.injectWithoutSemicolon( parts, urlPartIndex, randomString, urlPart );
|
|
|
|
}
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
parseImportLine( lineWords ) {
|
|
|
|
lineWords = this.trimParts( lineWords );
|
|
|
|
lineWords = this.removePreviousRandomNumber( lineWords );
|
|
|
|
lineWords = this.injectRandomNumber( lineWords );
|
|
|
|
return lineWords;
|
|
|
|
}
|
|
|
|
parseLine( lines, i ) {
|
|
|
|
var stripped = lines[i].replaceAll("\t", " ");
|
|
|
|
var parts = stripped.split(" ");
|
|
|
|
if( parts.includes("import") ) {
|
|
|
|
parts = this.parseImportLine( parts );
|
|
|
|
}
|
|
|
|
lines[i] = parts.join(" ");
|
|
|
|
}
|
|
|
|
parseSource( source, path ) {
|
|
|
|
source = source.replaceAll("\r", "\n");
|
|
|
|
var lines = source.split("\n");
|
|
|
|
for ( var i = 0; i < lines.length; i++ ) {
|
|
|
|
this.parseLine( lines, i );
|
|
|
|
|
|
}
|
|
|
|
var body = lines.join("\n");
|
|
|
|
return body;
|
|
|
|
}
|
|
|
|
} |