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.
matomo-injector/lib/script.js

80 lines
2.3 KiB
JavaScript
Raw Normal View History

2014-05-12 11:22:21 +02:00
(function() {
function injectScript(src, where) {
var elm = document.createElement('script');
elm.src = src;
document[where || 'head'].appendChild(elm);
2014-05-07 13:56:35 +02:00
}
2014-05-12 11:22:21 +02:00
var customjs = localStorage['customjs'];
if( customjs ) {
try {
customjs = JSON.parse(customjs);
2014-05-07 13:56:35 +02:00
}
2014-05-12 11:22:21 +02:00
catch(e) {
// Backward compatibility (version >1)
customjs = {
source: customjs,
config: {
enable: true,
include: '',
extra: ''
}
};
localStorage['customjs'] = JSON.stringify(customjs);
}
if( customjs.config.enable ) {
// Predefined include
if( customjs.config.include ) {
injectScript('https://ajax.googleapis.com/ajax/libs' + customjs.config.include);
}
// Extra include
var extra = (customjs.config.extra || '').split(';');
extra.forEach(function(line) {
if( line.substr(0, 1) !== '#' ) {
injectScript(line);
}
});
// Script
if( customjs.source || customjs.src ) {
setTimeout(function() {
// Backward compatibility (version 1)
if( customjs.src ) {
2014-05-12 11:25:14 +02:00
customjs.source = 'data:text/javascript,' + customjs.src;
2014-05-12 11:22:21 +02:00
}
2014-05-12 11:25:14 +02:00
2014-05-12 11:22:21 +02:00
injectScript(customjs.source, 'body');
}, 250);
}
2014-05-07 13:56:35 +02:00
}
}
2014-05-12 11:22:21 +02:00
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
switch(request.method) {
case 'getHost':
sendResponse({host: location.host});
break;
case 'setCustomJS':
localStorage['customjs'] = JSON.stringify(request.customjs);
case 'getCustomJS':
var customjs = JSON.parse(localStorage['customjs'] || '{}');
sendResponse({customjs: customjs});
break;
case 'removeCustomJS':
delete localStorage['customjs'];
break;
default:
sendResponse({src: '', config: {}});
}
if( request.reload ) {
window.location.reload();
}
});
})();