From 4022139bc79496eaff884f8535e858e866dc4483 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 24 Jun 2020 01:17:58 +0200 Subject: [PATCH] Major refactor and implemented communication with active tab through content script --- src/content.js | 19 ++++ src/manifest.json | 2 +- src/options/LocalCommands.vue | 194 ++++++++++++++-------------------- src/popup/App.vue | 2 +- src/utils.js | 41 ++++++- webpack.config.js | 1 + 6 files changed, 143 insertions(+), 116 deletions(-) create mode 100644 src/content.js diff --git a/src/content.js b/src/content.js new file mode 100644 index 0000000..67b1407 --- /dev/null +++ b/src/content.js @@ -0,0 +1,19 @@ +global.browser = require('webextension-polyfill'); + +browser.runtime.onMessage.addListener((message, sender, sendResponse) => { + switch (message.type) { + case 'getURL': + sendResponse(window.location.href); + break; + + case 'getBody': + sendResponse(document.body.innerHTML); + break; + + case 'setBody': + document.body.innerHTML = message.html; + break; + } +}); + +// vim:sw=2:ts=2:et: diff --git a/src/manifest.json b/src/manifest.json index 8dc4be5..529ea9c 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -26,7 +26,7 @@ "content_scripts": [ { "matches": ["*://*/*"], - "css": ["style.css"] + "js": ["content.js"] } ] } diff --git a/src/options/LocalCommands.vue b/src/options/LocalCommands.vue index 1ed2ab5..bad4576 100644 --- a/src/options/LocalCommands.vue +++ b/src/options/LocalCommands.vue @@ -1,118 +1,77 @@ @@ -137,7 +96,6 @@ export default { actions_: {}, scripts: {}, selectedAction: null, - selectedScript: null, selectedCategory: null, response: null, error: null, @@ -146,7 +104,7 @@ export default { computed: { actionsByHost() { - return Object.entries(this.actions_).reduce((obj, [name, action]) => { + return Object.entries({ ...this.actions_, ...this.scripts }).reduce((obj, [name, action]) => { const hosts = action.hosts || []; for (const host of hosts) { if (!(host in obj)) { @@ -227,11 +185,11 @@ export default { }, async removeScript() { - if (!this.selectedScript || !(this.selectedScript in this.scripts) || !confirm('Are you sure that you want to remove this script from this device?')) { + if (!this.selectedAction || !(this.selectedAction in this.scripts) || !confirm('Are you sure that you want to remove this script from this device?')) { return; } - const script = this.scripts[this.selectedScript]; + const script = this.scripts[this.selectedAction]; const hostIndex = script.hosts.indexOf(this.host); if (hostIndex < 0) { return; @@ -239,9 +197,9 @@ export default { script.hosts.splice(hostIndex, 1); if (script.hosts.length === 0) { - delete this.scripts[this.selectedScript]; + delete this.scripts[this.selectedAction]; } else { - this.scripts[this.selectedScript] = script; + this.scripts[this.selectedAction] = script; } await this.saveScripts(this.scripts); @@ -264,11 +222,11 @@ export default { }, async _runScript() { - if (!(this.selectedScript && this.host && this.selectedScript in this.scripts)) { + if (!(this.selectedAction && this.host && this.selectedAction in this.scripts)) { return; } - const script = this.scripts[this.selectedScript]; + const script = this.scripts[this.selectedAction]; this.error = null; try { @@ -282,14 +240,6 @@ export default { this.response = null; this.error = null; this.selectedAction = this.selectedAction === name ? null : name; - this.selectedScript = null; - }, - - toggleSelectedScript(name) { - this.response = null; - this.error = null; - this.selectedAction = null; - this.selectedScript = this.selectedScript === name ? null : name; }, }, @@ -304,6 +254,7 @@ export default {