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/popup.js

97 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-05-07 13:56:35 +02:00
chrome.tabs.getSelected(null, function(tab) {
var cjs = $('#cutomjs'),
tsc = $('textarea[name="editor"]', cjs);
var errorHandler = function() {
cjs.css({
"min-width": "120px",
"min-height": "20px",
"width": "350px",
"height": "80px"
});
cjs.removeClass('pure-g').html('<em><strong>It seems that this page cannot be modified with custom js...</strong><br><br> TIP: Try refresh page</em>');
};
var getConfig = function() {
return {
enable: $('input[name="enable"]').is(':checked'),
include: $('select[name="include"]').val()
};
};
var defaultContent = "/**\r\n Type Your custom JS code ... \r\n tip: you can include and use jQuery \r\n note: do not use // for comments \r\n**/\r\n\r\n";
/**
* Show current domain
*/
chrome.tabs.sendRequest(tab.id, {method: "getHost", reload: false}, function(response) {
console.log(response);
try {
$('input[name="domain"]', cjs).val(response.host);
}
catch(e) {
errorHandler();
}
});
/**
* Fill by local script
*/
chrome.tabs.sendRequest(tab.id, {method: "getCustomJS", reload: false}, function(response) {
var src;
if( response.customjs ) {
src = response.customjs.src ? decodeURI(response.customjs.src) : defaultContent;
if( response.customjs.config ) {
var config = response.customjs.config;
if( config.enable ) {
$('input[name="enable"]').attr('checked', 'checked');
}
if( config.include ) {
$('select[name="include"]', cjs).val(response.customjs.config.include);
}
}
}
tsc.val(src || defaultContent);
});
/**
* Enable on textarea change
*/
tsc.on('change keyup paste', function() {
$('input[name="enable"]', cjs).attr('checked', 'checked');
})
/**
* Save local script
*/
$('input[name="save"]', cjs).on('click', function() {
var src = encodeURI(tsc.val()),
customjs = {
src: src,
config: getConfig()
};
chrome.tabs.sendRequest(tab.id, {method: "setCustomJS", customjs: customjs, reload: true});
window.close();
})
/**
* Remove local script
*/
$('input[name="reset"]', cjs).on('click', function() {
tsc.val(decodeURI(defaultContent));
chrome.tabs.sendRequest(tab.id, {method: "removeCustomJS", reload: true});
window.close();
})
/**
* Set textarea as behave
*/
var editor = new Behave({
textarea: tsc[0],
tabSize: 3
});
});