/*jshint esversion: 6 */ /** * Get the log and display the data as table. */ var log = {}; /** * Reset the global log */ function resetGlobalLog(){ obj = {"log": []}; browser.runtime.sendMessage({ function: "setData", params: ['log', JSON.stringify(obj)] }); getLog(); location.reload(); } /** * Get the log and display to the user */ function getLog() { browser.runtime.sendMessage({ function: "getData", params: ['log'] }).then((data) => { log = data.response; // Sort the log | issue #70 log.log.sort(function(a,b) { return b.timestamp - a.timestamp; }); var length = Object.keys(log.log).length; var row; if(length != 0) { for(var i=0; i"+log.log[i].before+"" + ""+log.log[i].after+"" + ""+log.log[i].rule+"" + ""+toDate(log.log[i].timestamp)+""; $('#tbody').append(row); } } $('#logTable').DataTable({ "pageLength": 10, "language": { "url": getDataTableTranslation() } } ).order([3, 'desc']).draw(); }); } /** * Get the translation file for the DataTable */ function getDataTableTranslation() { var lang = browser.i18n.getUILanguage(); lang = lang.substring(0,2); var file = browser.extension.getURL('./external_js/dataTables/i18n/'+lang+'.lang'); return file; } /** * Convert timestamp to date */ function toDate(time) { return new Date(time).toLocaleString(); } /** * Load only when document is ready */ $(document).ready(function(){ setText(); getLog(); $('#reset_log_btn').on("click", resetGlobalLog); }); /** * Translate a string with the i18n API. * * @param {string} string Name of the attribute used for localization */ function translate(string) { return browser.i18n.getMessage(string); } /** * Set the text for the UI. */ function setText() { document.title = translate('log_html_page_title'); $('#page_title').text(translate('log_html_page_title')); $('#reset_log_btn').text(translate('log_html_reset_button')); $('#log_html_reset_button').prop('title', translate('log_html_reset_button_title')); $('#head_1').text(translate('log_html_table_head_1')); $('#head_2').text(translate('log_html_table_head_2')); $('#head_3').text(translate('log_html_table_head_3')); $('#head_4').text(translate('log_html_table_head_4')); } function handleError(error) { console.log(`Error: ${error}`); }