Skip to content
This repository was archived by the owner on Feb 24, 2021. It is now read-only.

Added global custom script #57

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Logs
logs
*.log
.vscode/*

# Runtime data
pids
Expand Down Expand Up @@ -29,5 +30,5 @@ build/Release
node_modules


# Visual Studio Code
# Visual Studio Code
.vs/*
101 changes: 101 additions & 0 deletions src/custom/global-keywords.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
window.debugCSSUsage = true

// Set our usage trackers
var globalUsageGet = 0;
var globalUsageSet = 0;

var globalProxy = {
get: function() { counts[0].count++; },
set: function() { counts[1].count++; },
configurable: true
}

var globalObjectUsageGet = 0;
var globalObjectUsageSet = 0;

var globalObjectProxy = {
get: function() { counts[2].count++; },
set: function() { counts[3].count++; },
configurable: true
}

var globalThisUsageGet = 0;
var globalThisUsageSet = 0;

var globalThisProxy = {
get: function() { counts[4].count++; },
set: function() { counts[5].count++; },
configurable: true
}

var globalsUsageGet = 0;
var globalsUsageSet = 0;

var globalsProxy = {
get: function() { counts[6].count++; },
set: function() { counts[7].count++; },
configurable: true
}

Object.defineProperty(window, 'globals', globalsProxy);
Object.defineProperty(window, 'globalThis', globalThisProxy);
Object.defineProperty(window, 'globalObject', globalObjectProxy);
Object.defineProperty(window, 'global', globalProxy);

var counts = [
{name: "globalUsageGet", count: globalUsageGet},
{name: "globalUsageSet", count: globalUsageSet},
{name: "globalObjectUsageGet", count: globalObjectUsageGet},
{name: "globalObjectUsageSet", count: globalObjectUsageSet},
{name: "globalThisUsageGet", count: globalThisUsageGet},
{name: "globalThisUsageSet", count: globalThisUsageSet},
{name: "globalsUsageGet", count: globalsUsageGet},
{name: "globalsUsageSet", count: globalsUsageSet}
]

// Define them so that we can determine if they get stomped on

void function() {
document.addEventListener('DOMContentLoaded', function () {

var results = new Array();
counts.forEach(function(i) {
if(i.count != 0) {
results.push({"name":i.name, "count": i.count, "href": location.href });
}
});

appendResults(results);

// Add it to the document dom
function appendResults(results) {
if(window.debugCSSUsage) console.log("Trying to append");
var output = document.createElement('script');
output.id = "css-usage-tsv-results";
output.textContent = convertToTSV(results);
output.type = 'text/plain';
document.querySelector('head').appendChild(output);
var successfulAppend = checkAppend();
}

function checkAppend() {
if(window.debugCSSUsage) console.log("Checking append");
var elem = document.getElementById('css-usage-tsv-results');
if(elem === null) {
if(window.debugCSSUsage) console.log("Element not appended");
}
else {
if(window.debugCSSUsage) console.log("Element successfully found");
}
}

function convertToTSV(results) {
const replacer = (key, value) => value === null ? '' : value // specify how you want to handle null values here
const header = Object.keys(results[0])
let csv = results.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join('\t'))
csv.unshift(header.join('\t'))
csv = csv.join('\r\n')
return csv;
}
});
}();
19 changes: 19 additions & 0 deletions tests/custom/global-keywords.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Testing globals usage</title>
<script src="../../src/custom/global-keywords.js"></script>
</head>
<body>
<script>
var global = "one";
var globalObject = "two";
var globalThis = "three";
var globals = "four";
console.log(global);
console.log(globalObject);
console.log(globalThis);
console.log(globals);
</script>
</body>
</html>