« get me outta code hell

Auto-resolve path where possible - interactive-bgm - Browser extension that adds background music based on the site you're browsing
about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlorrie <towerofnix@gmail.com>2019-03-22 18:01:33 -0300
committerFlorrie <towerofnix@gmail.com>2019-03-22 18:01:33 -0300
commit9652e73c5144939fb2aee9e0a3cffee9fc8249b0 (patch)
treef4ccb8f4f0ec68a2c02b42bffbc9d8b16421d7bb
parent15de6776a959c0b267b6f601417caf1f973b3b38 (diff)
Auto-resolve path where possible
-rw-r--r--README.md4
-rw-r--r--index.js113
-rwxr-xr-xnative-app/index.js20
3 files changed, 9 insertions, 128 deletions
diff --git a/README.md b/README.md
index f8e1753..13fb0c7 100644
--- a/README.md
+++ b/README.md
@@ -4,8 +4,6 @@ Adds background music to your browsing.
 
 ## Installation
 
-Modfy `native-app/index.js` to have the proper paths and filenames.
-
-Copy (or link) `native-app/interactive_bgm.json` into `~/.mozilla/native-messaging-hosts/`. Open `about:debugging`, click "Load Temporary Add-on...", and pick `extension/manifest.json`.
+Edit `native-app/interactive_bgm.json` to contain the full path of this directory on your system. Then copy (or link) that file into `~/.mozilla/native-messaging-hosts/`. Open `about:debugging`, click "Load Temporary Add-on...", and pick `extension/manifest.json`. Music should automatically start playing after a few seconds.
 
 (The music files included here, while WIP, are ripped from Hollow Knight.)
diff --git a/index.js b/index.js
deleted file mode 100644
index 72beebf..0000000
--- a/index.js
+++ /dev/null
@@ -1,113 +0,0 @@
-const { spawn } = require('child_process');
-const FIFO = require('fifo-js');
-const http = require('http');
-
-class TrackPlayer {
-    constructor(file) {
-        this.file = file;
-        this.volume = 0;
-        this.storedVolume = this.volume;
-    }
-
-    loadProcess() {
-        this.fifo = new FIFO();
-        this.process = spawn('mpv', [
-            '--no-video',
-            '--loop',
-            '--volume=' + this.volume,
-            '--input-file=' + this.fifo.path,
-            this.file,
-        ]);
-
-        this.process.stderr.pipe(process.stderr);
-    }
-
-    sendCommand(command) {
-        if (this.fifo) {
-            this.fifo.write(command);
-        }
-    }
-
-    seekToStart() {
-        this.sendCommand('seek 0 absolute');
-    }
-
-    pause() {
-        this.sendCommand('set pause yes');
-    }
-
-    play() {
-        this.sendCommand('set pause no');
-    }
-
-    setVolume(volume) {
-        if (Math.floor(volume) !== this.storedVolume) {
-            this.storedVolume = Math.floor(volume);
-            this.sendCommand(`set volume ${volume}`);
-        }
-        this.volume = volume;
-    }
-}
-
-const tracks = {
-    mantis: new TrackPlayer('track1.wav'),
-    bass: new TrackPlayer('track2.wav'),
-    main: new TrackPlayer('track3.wav')
-};
-
-for (const track of Object.values(tracks)) {
-    track.loadProcess();
-    track.pause();
-}
-
-setTimeout(() => {
-    for (const track of Object.values(tracks)) {
-        track.seekToStart();
-        track.play();
-    }
-
-    let targetMode = [
-        {track: 'main', volume: 100}
-    ];
-
-    setInterval(() => {
-        if (!Array.isArray(targetMode)) {
-            console.log('targetMode is not an array.');
-            targetMode = [];
-        }
-
-        for (const [key, track] of Object.entries(tracks)) {
-            const mode = targetMode.find(m => m.track === key) || {volume: 0};
-            track.setVolume(track.volume + 0.1 * (mode.volume - track.volume));
-        }
-    }, 100);
-
-    const server = http.createServer((request, response) => {
-        response.setHeader('Access-Control-Allow-Origin', '*');
-        response.setHeader('Access-Control-Allow-Headers', 'Authorization, Cache-Control, Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers');
-        console.log('Got request.');
-
-        if (request.method === 'GET') {
-            response.write('Please use POST.');
-            response.end();
-        } else if (request.method === 'POST') {
-            let allData = '';
-            request.on('data', data => {
-                allData += data;
-            });
-            request.on('end', () => {
-                try {
-                    targetMode = JSON.parse(allData);
-                    response.write('Set.');
-                } catch (error) {
-                    response.write('Invalid JSON.');
-                }
-                response.end();
-            });
-        } else {
-            response.end();
-        }
-    });
-
-    server.listen(8000);
-}, 250);
diff --git a/native-app/index.js b/native-app/index.js
index 2f174ea..16ef810 100755
--- a/native-app/index.js
+++ b/native-app/index.js
@@ -1,28 +1,24 @@
 #!/usr/bin/env node
 
-const basePath = '/home/florrie/Documents/interactive-bgm';
-
-const logFile = basePath + '/native-app/log';
-
-const log = msg => {
-    require('fs').appendFileSync(logFile, msg + '\n');
-};
-
-
-log('Started ' + Date());
-
 const { spawn } = require('child_process');
 const EventEmitter = require('events');
 const FIFO = require('fifo-js');
 const http = require('http');
+const path = require('path');
+const fs = require('fs');
 
-log('Loaded modules');
+const basePath = path.resolve(__dirname, '..');
+const logFile = basePath + '/native-app/log';
+const log = msg => fs.appendFileSync(logFile, msg + '\n');
+
+log('Started ' + Date());
 
 class TrackPlayer {
     constructor(file) {
         this.file = file;
         this.volume = 0;
         this.storedVolume = this.volume;
+        log('Created track: ' + file);
     }
 
     loadProcess() {