1
0
Fork 0
This repository has been archived on 2024-06-28. You can view files and clone it, but cannot push or open issues or pull requests.
piwik-checker/inject.js

73 lines
2.3 KiB
JavaScript
Raw Normal View History

2017-07-06 14:35:58 +02:00
tests = {
errors: [],
piwikJSScriptObject: function() {
var allElements = document.getElementsByTagName('script');
for (var i = 0, n = allElements.length; i < n; i++) {
if (allElements[i].hasAttribute("src") && allElements[i].getAttribute("src").endsWith("piwik.js")) {// TODO: support renamed piwik.js
return allElements[i];
}
}
},
2017-07-07 19:27:23 +02:00
parseURL: function(url) {
var a = document.createElement('a');
a.href = 'https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=picture%20of%20a%20potato';
// document.removeChild(a);
return {
protocol: a.protocol,
host: a.host,
hostname: a.hostname,
port: a.port,
pathname: a.pathname,
search: a.search
};
},
2017-07-07 22:54:29 +02:00
isPageHTTPS: function() {
return location.protocol === "https:"
},
usesPiwik: function() {
return !!this.piwikJSScriptObject();
},
// -------------
2017-07-06 14:35:58 +02:00
/**
* @return {string}
*/
URLtoPiwikJS: function() {
return this.scriptObject.getAttribute("src");
},
2017-07-07 19:27:23 +02:00
piwikUsesHTTPS: function() {
2017-07-07 22:54:29 +02:00
return this.parseURL(this.URLtoPiwikJS()).protocol === "https:"
2017-07-07 19:27:23 +02:00
},
isURLprotocolRelative: function() {
return this.URLtoPiwikJS().startsWith("//");
},
2017-07-07 22:54:29 +02:00
isMixedContent: function() {
return this.isPageHTTPS() && !this.piwikUsesHTTPS();
},
2017-07-06 14:35:58 +02:00
isScriptAsync: function() {
return this.scriptObject.hasAttribute("async") && this.scriptObject.hasAttribute("defer")
},
2017-07-07 22:54:29 +02:00
isPageUTF8: function() {
return document.characterSet.toLowerCase() === "utf-8";
},
2017-07-06 14:35:58 +02:00
main: function() {
this.scriptObject = this.piwikJSScriptObject();
if (!this.scriptObject) {
console.warn("No piwik found");
return false;
}
2017-07-06 16:50:41 +02:00
return {
UrltoPiwikJs: this.URLtoPiwikJS(),
2017-07-07 19:27:23 +02:00
piwikUsesHTTPS: this.piwikUsesHTTPS(),
isURLprotocolRelative: this.isURLprotocolRelative(),
2017-07-07 22:54:29 +02:00
isMixedContent: this.isMixedContent(),
2017-07-07 19:27:23 +02:00
isScriptAsync: this.isScriptAsync(),
2017-07-07 22:54:29 +02:00
isPageUTF8: this.isPageUTF8()
2017-07-06 14:35:58 +02:00
};
}
};
2017-07-07 22:54:29 +02:00
chrome.runtime.sendMessage({action: "test", usesPiwik: tests.usesPiwik()});
// willBeSendToBackgroundJs = tests.main();