Skip to content

Commit

Permalink
Feita injeção de JS no html.
Browse files Browse the repository at this point in the history
  • Loading branch information
JsThiago committed Aug 21, 2020
1 parent 11189c6 commit 99a4d91
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 27 deletions.
14 changes: 1 addition & 13 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat simples</title>
<title>fast refresh test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js"></script>
<link rel="stylesheet" href="styles.css">
Expand All @@ -18,19 +18,7 @@
<input type="text" name="message" placeholder="Digite sua mensagem">
<button id="button" type="submit">Enviar</button>
</form>
<script type="text/javascript">
console.log(this);
var socket = io('http://localhost:3333');
socket.on('refresh', data => {

location.reload(true);
socket.emit('refreshed');
})
$('#button').click(() => {
socket.emit('refreshed')
consoole.log('aadas');
})
</script>
</body>

</html>
5 changes: 3 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const logDebugger = require('./src/logsDebugger');
const socket = require('./src/socket');
const app = require('./src/app.js');

const childProcess = require('child_process');


socket(app);
logDebugger.connected();
logDebugger.connected();
childProcess.exec('start http://localhost:3333/fr');
11 changes: 4 additions & 7 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ const express = require('express');
const path = require('path');
const app = express();
const logDebugger = require('./logsDebugger');

const fsPromise = require('./util/fsPromise.js');
logDebugger.connecting();
app.use(express.static(path.join(__dirname, '..', 'public')));




app.use('/', (req, res) => {
return res.send(path.join(__dirname, '..', 'public', 'index.html'));
app.use('/fr', async (req, res) => {
const html = await fsPromise();
return res.send(html);
});


Expand Down
5 changes: 0 additions & 5 deletions src/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@ fs.watch(path.join(__dirname, '..', 'public'), (eventType, filename) => {

function socket(server) {



const io = require('socket.io')(server);

io.on('connection', socket => {
socketRef = socket
socket.on('refreshed', (data) => {
console.log('refresh complete');
});
})
return socketRef;
}
Expand Down
24 changes: 24 additions & 0 deletions src/util/fsPromise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const path = require('path');
const fs = require('fs')
module.exports = function fsPromise() {
const regexPattern = /<\/body>/i;
const jsInject = `
<script>
console.log('inject completed')
var socket = io('http://localhost:3333');
socket.on('refresh', data => {
location.reload(true);
socket.emit('refreshed');
})
</script>
`
return new Promise((resolve, reject) => {
fs.readFile(path.join(__dirname, '..', '..', 'public', 'index.html'), (err, data) => {
resolve(data.toString().replace(regexPattern,
`${jsInject}
</body>
`))
})
})
}

0 comments on commit 99a4d91

Please sign in to comment.