Reputation: 221
Is there anything I can do to prevent users to run js function which is in file from console ? I have this getReward
function here:
function getReward(){
$('.reward-button').remove();
$('#rewardBtn').remove();
document.getElementById("fightInfo").innerHTML =
'<strong>You</strong> won the fight.<br><strong>Reward:</strong><br>Gold: ' +gold+' <img src="/img/gold.png" style="margin-top: -1%"><br>EXP: '+exp+'<br>'
+
'<br>'
+
'<button style="font-size: 24px;" class="btn btn-danger" onclick="backToArena();">⚔️<br>Back To Arena</button>';
socket.emit('win-fight', {
gold: gold,
exp: exp,
username: userName,
enemyname: enemyName
});
}
function backToArena(){
window.location.href = "/arena";
}
The problem is that user can just type in console getReward();
and he automatically get's the reward because then socket works and it goes to server side from client side. Is there anyway to prevent this ?
UPDATE
document.getElementById("rewardBtn").innerHTML =
'<button style="background-color: blue; font-size: 24px;" class="btn btn-primary" id="reward-button">Get reward 🏆</button>';
And then I do this:
document.querySelector('#reward-button').addEventListener('click', getReward);
But this doesn't run getReward function nothing happens no error.
Upvotes: 4
Views: 1747
Reputation: 1
here is magic
"use strict";
!function() {
function detectDevTool(allow) {
if(isNaN(+allow)) allow = 100;
var start = +new Date();
debugger;
var end = +new Date();
if(isNaN(start) || isNaN(end) || end - start > allow) {
consoleCheck();
}
}
if(window.attachEvent)
{
if (document.readyState === "complete" || document.readyState === "interactive")
{
detectDevTool();
window.attachEvent('onresize', detectDevTool);
window.attachEvent('onmousemove', detectDevTool);
window.attachEvent('onfocus', detectDevTool);
window.attachEvent('onblur', detectDevTool);
}
else
{
setTimeout(argument.callee, 0);
}
}
else
{
window.addEventListener('load', detectDevTool);
window.addEventListener('resize', detectDevTool);
window.addEventListener('mousemove', detectDevTool);
window.addEventListener('focus', detectDevTool);
window.addEventListener('blur', detectDevTool);
}
}();
function consoleCheck()
{
document.querySelector('body').innerHTML = 'We strongly support to not using console for extra actions!';
}
Just add this script in your project it will definitely stops inspecting.
Upvotes: 0
Reputation: 370769
Simply wrap the whole script in an IIFE (Immediately Invoked Function Expression) so that getReward
(and all your other functions) are not on the top level. Functions (and other variables) on the top level are automatically assigned to window
, and are thus callable by simply typing the function name into the console. But, if a function is declared inside another function, the inner function won't be assigned to the global object.
(() => {
function getReward(){
$('.reward-button').remove();
$('#rewardBtn').remove();
document.getElementById("fightInfo").innerHTML =
'<strong>You</strong> won the fight.<br><strong>Reward:</strong><br>Gold: ' +gold+' <img src="/img/gold.png" style="margin-top: -1%"><br>EXP: '+exp+'<br>'
+
'<br>'
+
'<button style="font-size: 24px;" class="btn btn-danger" onclick="backToArena();">⚔️<br>Back To Arena</button>';
socket.emit('win-fight', {
gold: gold,
exp: exp,
username: userName,
enemyname: enemyName
});
}
function backToArena(){
window.location.href = "/arena";
}
})();
Wrapping your scripts in an IIFE is useful not only for security (not good security, but better than open-console-and-type-function insecurity), but also for the sake of avoiding pollution of the global namespace, which is best avoided when possible.
Upvotes: 4