Path: blob/master/extensions/admin_ui/media/javascript/ui/panel/AutoRunTab.js
1155 views
const defaultRule = {1"name": "Display an alert",2"author": "mgeeky",3"modules": [4{5"name": "alert_dialog",6"condition": null,7"options": {8"text":"You've been BeEFed ;>"9}10}11],12"execution_order": [0],13"execution_delay": [0],14"chain_mode": "nested-forward"15};16171819/**20* Asynchronously returns the currently active rules in an array.21* Empty array means no rules are active.22* null if there was an error.23*/24getCurrentRules = async function(token) {2526try {27var res = await fetch(`/api/autorun/rules?token=${token}`);28if (!res.ok) {29throw new Error(`Getting auto run rules failed with status ${res.status}`);30}31const data = await res.json();32const rules = JSON.parse(data.rules);3334if (data.success === true && Array.isArray(rules)) {35return rules;36}3738console.log("No active auto run rules.");39return [];4041} catch(error) {42console.error(error);43console.error("Failed to get auto run rules.");44return null;45}46}4748getModules = async function(token) {49try {50var res = await fetch(`/api/modules?token=${token}`);51if (!res.ok) {52throw new Error(`Getting auto run rules failed with status ${res.status}`);53}54const modules = await res.json();5556return modules;5758} catch(error) {59console.error(error);60console.error("Failed to get auto run rules.");61return null;62}63}6465AutoRunTab = function() {66// RESTful API token.67var token = BeefWUI.get_rest_token();6869// Heading container to describe general Auto Run state.70var ruleLoadingState = new Ext.Container({71html: "<p>Loading Auto Run rules...</p>",72})73var headingContainer = new Ext.Panel({74style: {75font: '11px tahoma,arial,helvetica,sans-serif'76},77padding:'10 10 10 10',78border: false,79items: [{80xtype: 'container',81html: '\82<div>\83<h4>Auto Run Rules</h4>\84<p>These determine what commands run automatically when a browser is hooked.</p>\85</div>'86},87ruleLoadingState,88{89xtype: 'button',90text: 'Add New Rule',91handler: addRule92}],93listeners: {94afterrender: loadRules95}96});97// Contains all of the rules and inputs to change each rule.98var ruleContainer = new Ext.Panel({99border: false,100listeners: {101afterrender: loadRules102}103});104105async function deleteRule(id) {106const res = await fetch(`/api/autorun/rule/${id}?token=${token}`, {method: 'DELETE'});107if (!res.ok) {108console.error(`Failed when deleting rule with id ${id}. Failed with status ${res.status}.`);109return;110}111// Update the entire rules panel. Not very efficient.112loadRules();113}114115async function addRule() {116const res = await fetch(`/api/autorun/rule/add?token=${token}`, {117method: 'POST',118headers: {'Content-Type': 'application/json'},119body: JSON.stringify(defaultRule)120});121if (!res.ok) {122console.error(`Failed when adding a new rule with status ${res.status}.`);123return;124}125// Update the entire rules panel. Not very efficient.126loadRules();127}128129async function updateRule(id, newRuleData) {130const res = await fetch(`/api/autorun/rule/${id}?token=${token}`, {131method: 'PATCH',132headers: {'Content-Type': 'application/json'},133body: JSON.stringify(newRuleData)134});135if (!res.ok) {136console.error(`Failed when adding a new rule with status ${res.status}.`);137return;138}139// Update the entire rules panel. Not very efficient.140loadRules();141}142143async function loadRules() {144let modules = [];145let rules = [];146try {147modules = await getModules(token);148rules = await getCurrentRules(token);149} catch (error) {150console.error(error);151console.error("Failed to load command modules and/or rules for Auto Run.");152ruleLoadingState.update("<p>Failed to load Auto Run rules.</p>");153return;154}155156if (rules !== null) {157ruleLoadingState.update(`<p>Loaded ${rules.length} Auto Run rules.</p>`);158ruleContainer.removeAll();159160for (let i = 0; i < rules.length; i++) {161ruleForm = new AutoRunRuleForm(162rules[i],163modules,164function() {deleteRule(rules[i].id)},165function(newRuleData) {updateRule(rules[i].id, newRuleData)}166);167ruleContainer.add(ruleForm);168}169ruleContainer.doLayout();170} else {171ruleLoadingState.update("<p>Failed to load Auto Run rules.</p>");172}173}174175AutoRunTab.superclass.constructor.call(this, {176region: 'center',177items: [headingContainer, ruleContainer],178autoScroll: true,179border: false,180closable: false181});182};183184Ext.extend(AutoRunTab, Ext.Panel, {});185186