在Google?Chrome瀏覽器中,按F12進(jìn)入的是本地調(diào)試界面。
創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設(shè),燈塔企業(yè)網(wǎng)站建設(shè),燈塔品牌網(wǎng)站建設(shè),網(wǎng)站定制,燈塔網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,燈塔網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
在F12調(diào)試界面中修改的JS代碼,是修改的“本地緩存文件”中的代碼,而不是存在于服務(wù)器上的網(wǎng)頁的真正代碼,網(wǎng)頁的真正代碼在瀏覽器中是無法修改的。
Chrome開發(fā)者調(diào)試工具(F12),可以在調(diào)試界面修改代碼(包括JS、HTML和CSS)模擬本地運(yùn)行,具體步驟大致如下:
在需要修改的代碼段上方合理代碼行設(shè)置斷點(diǎn);
按F5鍵刷新網(wǎng)頁,并等待網(wǎng)頁執(zhí)行到斷點(diǎn);
修改JS代碼或其它需要修改的代碼,修改后選擇單步執(zhí)行或繼續(xù)執(zhí)行即可。
通常,調(diào)試中修改無效,是因?yàn)榫W(wǎng)頁加載完成,其實(shí)就是運(yùn)行已經(jīng)完成了,修改代碼不會(huì)再次執(zhí)行。
以 iphone 上的自帶的 safari 瀏覽器為例:
1、找到 iphone 的 “設(shè)置” App
2、點(diǎn)擊進(jìn)入,下拉找到 “Safari 瀏覽器”并點(diǎn)擊進(jìn)入
3、進(jìn)入后下拉至底部找到 “高級(jí)”并點(diǎn)擊進(jìn)入
4、進(jìn)入后可看到“JavaScript”選項(xiàng),點(diǎn)擊右側(cè)的綠色區(qū)塊可關(guān)閉 JavaScript 功能
span style="font-family:Arial, Helvetica, sans-serif;"'use strict';/spanvar CryptoJS = require("crypto-js");var express = require("express");var bodyParser = require('body-parser');var WebSocket = require("ws");var http_port = process.env.HTTP_PORT || 3001;var p2p_port = process.env.P2P_PORT || 6001;var initialPeers = process.env.PEERS ? process.env.PEERS.split(',') : [];class Block { constructor(index, previousHash, timestamp, data, hash) { this.index = index; this.previousHash = previousHash.toString(); this.timestamp = timestamp; this.data = data; this.hash = hash.toString(); }}var sockets = [];var MessageType = { QUERY_LATEST: 0, QUERY_ALL: 1, RESPONSE_BLOCKCHAIN: 2};var getGenesisBlock = () = { return new Block(0, "0", 1465154705, "my genesis block!!", "816534932c2b7154836da6afc367695e6337db8a921823784c14378abed4f7d7");};var blockchain = [getGenesisBlock()];var initHttpServer = () = { var app = express(); app.use(bodyParser.json()); app.get('/blocks', (req, res) = res.send(JSON.stringify(blockchain))); app.post('/mineBlock', (req, res) = { var newBlock = generateNextBlock(req.body.data); addBlock(newBlock); broadcast(responseLatestMsg()); console.log('block added: ' + JSON.stringify(newBlock)); res.send(); }); app.get('/peers', (req, res) = { res.send(sockets.map(s = s._socket.remoteAddress + ':' + s._socket.remotePort)); }); app.post('/addPeer', (req, res) = { connectToPeers([req.body.peer]); res.send(); }); app.listen(http_port, () = console.log('Listening http on port: ' + http_port));};var initP2PServer = () = { var server = new WebSocket.Server({port: p2p_port}); server.on('connection', ws = initConnection(ws)); console.log('listening websocket p2p port on: ' + p2p_port);};var initConnection = (ws) = { sockets.push(ws); initMessageHandler(ws); initErrorHandler(ws); write(ws, queryChainLengthMsg());};var initMessageHandler = (ws) = { ws.on('message', (data) = { var message = JSON.parse(data); console.log('Received message' + JSON.stringify(message)); switch (message.type) { case MessageType.QUERY_LATEST: write(ws, responseLatestMsg()); break; case MessageType.QUERY_ALL: write(ws, responseChainMsg()); break; case MessageType.RESPONSE_BLOCKCHAIN: handleBlockchainResponse(message); break; } });};var initErrorHandler = (ws) = { var closeConnection = (ws) = { console.log('connection failed to peer: ' + ws.url); sockets.splice(sockets.indexOf(ws), 1); }; ws.on('close', () = closeConnection(ws)); ws.on('error', () = closeConnection(ws));};var generateNextBlock = (blockData) = { var previousBlock = getLatestBlock(); var nextIndex = previousBlock.index + 1; var nextTimestamp = new Date().getTime() / 1000; var nextHash = calculateHash(nextIndex, previousBlock.hash, nextTimestamp, blockData); return new Block(nextIndex, previousBlock.hash, nextTimestamp, blockData, nextHash);};var calculateHashForBlock = (block) = { return calculateHash(block.index, block.previousHash, block.timestamp, block.data);};var calculateHash = (index, previousHash, timestamp, data) = { return CryptoJS.SHA256(index + previousHash + timestamp + data).toString();};var addBlock = (newBlock) = { if (isValidNewBlock(newBlock, getLatestBlock())) { blockchain.push(newBlock); }};var isValidNewBlock = (newBlock, previousBlock) = { if (previousBlock.index + 1 !== newBlock.index) { console.log('invalid index'); return false; } else if (previousBlock.hash !== newBlock.previousHash) { console.log('invalid previoushash'); return false; } else if (calculateHashForBlock(newBlock) !== newBlock.hash) { console.log(typeof (newBlock.hash) + ' ' + typeof calculateHashForBlock(newBlock)); console.log('invalid hash: ' + calculateHashForBlock(newBlock) + ' ' + newBlock.hash); return false; } return true;};var connectToPeers = (newPeers) = { newPeers.forEach((peer) = { var ws = new WebSocket(peer); ws.on('open', () = initConnection(ws)); ws.on('error', () = { console.log('connection failed') }); });};var handleBlockchainResponse = (message) = { var receivedBlocks = JSON.parse(message.data).sort((b1, b2) = (b1.index - b2.index)); var latestBlockReceived = receivedBlocks[receivedBlocks.length - 1]; var latestBlockHeld = getLatestBlock(); if (latestBlockReceived.index latestBlockHeld.index) { console.log('blockchain possibly behind. We got: ' + latestBlockHeld.index + ' Peer got: ' + latestBlockReceived.index); if (latestBlockHeld.hash === latestBlockReceived.previousHash) { console.log("We can append the received block to our chain"); blockchain.push(latestBlockReceived); broadcast(responseLatestMsg()); } else if (receivedBlocks.length === 1) { console.log("We have to query the chain from our peer"); broadcast(queryAllMsg()); } else { console.log("Received blockchain is longer than current blockchain"); replaceChain(receivedBlocks); } } else { console.log('received blockchain is not longer than received blockchain. Do nothing'); }};var replaceChain = (newBlocks) = { if (isValidChain(newBlocks) newBlocks.length blockchain.length) { console.log('Received blockchain is valid. Replacing current blockchain with received blockchain'); blockchain = newBlocks; broadcast(responseLatestMsg()); } else { console.log('Received blockchain invalid'); }};var isValidChain = (blockchainToValidate) = { if (JSON.stringify(blockchainToValidate[0]) !== JSON.stringify(getGenesisBlock())) { return false; } var tempBlocks = [blockchainToValidate[0]]; for (var i = 1; i blockchainToValidate.length; i++) { if (isValidNewBlock(blockchainToValidate[i], tempBlocks[i - 1])) { tempBlocks.push(blockchainToValidate[i]); } else { return false; } } return true;};var getLatestBlock = () = blockchain[blockchain.length - 1];var queryChainLengthMsg = () = ({'type': MessageType.QUERY_LATEST});var queryAllMsg = () = ({'type': MessageType.QUERY_ALL});var responseChainMsg = () =({ 'type': MessageType.RESPONSE_BLOCKCHAIN, 'data': JSON.stringify(blockchain)});var responseLatestMsg = () = ({ 'type': MessageType.RESPONSE_BLOCKCHAIN, 'data': JSON.stringify([getLatestBlock()])});var write = (ws, message) = ws.send(JSON.stringify(message));var broadcast = (message) = sockets.forEach(socket = write(socket, message));connectToPeers(initialPeers);initHttpServer();initP2PServer();