2017-07-30 12 views
2

Google ChromeのアドレスバーにJavaScriptコードを実行したいjavascript:Google ChromeのアドレスバーでJavaScript機能を実行

javascript:alert("aaa"); 

ただし、複数のアラートでは機能しません。

javascript:{alert("aaa"); alert("blabla"); document.getElementsById("myId").innerHTML = "aa";} 

しかし、それは、そのようなforや関数を呼び出すなど、より複雑なものでは動作しません:私は、括弧内のすべてを置くとトリックを行うだろうということが分かりました。私は本当にこのようなforを実行する必要があります。

var elements = document.getElementsByTagName('div'); 

for (var i = 0; i < elements.length; i++) { 
    elements[i].innerHTML = "foo"; 
}​ 

私は} ... javascript:javascript:{の両方でそれを試してみましたし、それが動作しませんでした。

私の質問は、上記のようなもっと複雑なJavaScriptコードを実行するにはどうすればよいですか?

+0

はhttps://jscompress.com/のような縮小化ツールを使用して、あなたのコードを実行し、それが動作します「javascript:」と言っても – Buzzy

答えて

0

コードが有効でない理由は、有効な戻り値があるからです。したがって、elements[i].innerHTML = "foo"のような文を実行すると、値fooが返されます。ブラウザはDOMを更新しますが、ステートメントの結果も表示する必要があります。その結果、ページ全体が直ちに戻り値に置き換えられます。

void(0)の戻り値がundefinedであるため、@ Djordjeの答えに示唆されているようにvoid(0)を配置するソリューションが機能します。したがって、ブラウザはその値を表示する必要がなくなり、更新されたDOMを見ることができます。

私が答えに気づく前に、私は次のコードを実行しようとしていました。戻り値もundefinedであるため、これも機能します。

{ 
    var elements = document.getElementsByTagName('div'); 

    for (var i = 0; i < elements.length; i++) { 
     elements[i].innerHTML = "foo"; 
    } 

    undefined; 
} 

この効果をよりよく確認するには、DOMの更新直後にアラート文を追加し、有効な値を返すことができます。このように:

{ 
    var elements = document.getElementsByTagName('div'); 

    for (var i = 0; i < elements.length; i++) { 
     elements[i].innerHTML = "foo"; 
    } 

    alert("The DOM update statements have executed."); 
    "You can see that now the DOM is replaced entirely."; 
} 
+0

ありがとう。私はこのための解決策があるのでとても嬉しいです:) –

0

複数行のコードを受け入れないようです。ですから、私が細工をして1本のラインにすべてを敷き詰めたとき、それは機能しました。たとえば、次のコードは、ページの右上にFPSカウンタ(秒あたりのフレーム数)を入れます。

javascript:!function(){var t=document.createElement("script");!function(t,i){function e(t,i){if(null==i)return null;null==i.__id__&&(i.__id__=h++);var e;return null==t.hx__closures__?t.hx__closures__={}:e=t.hx__closures__[i.__id__],null==e&&(e=function(){return e.method.apply(e.scope,arguments)},e.scope=t,e.method=i,t.hx__closures__[i.__id__]=e),e}var s=("undefined"!=typeof window?window:exports).Perf=function(t,i){null==i&&(i=0),null==t&&(t="TR"),this._perfObj=window.performance,null!=n.field(this._perfObj,"memory")&&(this._memoryObj=n.field(this._perfObj,"memory")),this._memCheck=null!=this._perfObj&&null!=this._memoryObj&&0<this._memoryObj.totalJSHeapSize,this._pos=t,this._offset=i,this.currentFps=60,this.currentMs=0,this.currentMem="0",this.avgFps=this.lowFps=60,this._ticks=this._time=this._totalFps=this._measureCount=0,this._fpsMax=this._fpsMin=60,null!=this._perfObj&&null!=(o=this._perfObj,e(o,o.now))?this._startTime=this._perfObj.now():this._startTime=(new Date).getTime(),this._prevTime=-s.MEASUREMENT_INTERVAL,this._createFpsDom(),this._createMsDom(),this._memCheck&&this._createMemoryDom(),null!=(o=window,e(o,o.requestAnimationFrame))?this.RAF=(o=window,e(o,o.requestAnimationFrame)):null!=window.mozRequestAnimationFrame?this.RAF=window.mozRequestAnimationFrame:null!=window.webkitRequestAnimationFrame?this.RAF=window.webkitRequestAnimationFrame:null!=window.msRequestAnimationFrame&&(this.RAF=window.msRequestAnimationFrame),null!=(o=window,e(o,o.cancelAnimationFrame))?this.CAF=(o=window,e(o,o.cancelAnimationFrame)):null!=window.mozCancelAnimationFrame?this.CAF=window.mozCancelAnimationFrame:null!=window.webkitCancelAnimationFrame?this.CAF=window.webkitCancelAnimationFrame:null!=window.msCancelAnimationFrame&&(this.CAF=window.msCancelAnimationFrame),null!=this.RAF&&(this._raf=n.callMethod(window,this.RAF,[e(this,this._tick)]))};s.prototype={_init:function(){this.currentFps=60,this.currentMs=0,this.currentMem="0",this.avgFps=this.lowFps=60,this._ticks=this._time=this._totalFps=this._measureCount=0,this._fpsMax=this._fpsMin=60,null!=this._perfObj&&null!=(o=this._perfObj,e(o,o.now))?this._startTime=this._perfObj.now():this._startTime=(new Date).getTime(),this._prevTime=-s.MEASUREMENT_INTERVAL},_now:function(){return null!=this._perfObj&&null!=(o=this._perfObj,e(o,o.now))?this._perfObj.now():(new Date).getTime()},_tick:function(t){var i;i=null!=this._perfObj&&null!=(o=this._perfObj,e(o,o.now))?this._perfObj.now():(new Date).getTime(),this._ticks++,null!=this._raf&&i>this._prevTime+s.MEASUREMENT_INTERVAL&&(this.currentMs=Math.round(i-this._startTime),this.ms.innerHTML="MS: "+this.currentMs,this.currentFps=Math.round(1e3*this._ticks/(i-this._prevTime)),0<this.currentFps&&t>s.DELAY_TIME&&(this._measureCount++,this._totalFps+=this.currentFps,this.lowFps=this._fpsMin=Math.min(this._fpsMin,this.currentFps),this._fpsMax=Math.max(this._fpsMax,this.currentFps),this.avgFps=Math.round(this._totalFps/this._measureCount)),this.fps.innerHTML="FPS: "+this.currentFps+" ("+this._fpsMin+"-"+this._fpsMax+")",this.fps.style.backgroundColor=30<=this.currentFps?s.FPS_BG_CLR:15<=this.currentFps?s.FPS_WARN_BG_CLR:s.FPS_PROB_BG_CLR,this._prevTime=i,this._ticks=0,this._memCheck&&(this.currentMem=this._getFormattedSize(this._memoryObj.usedJSHeapSize,2),this.memory.innerHTML="MEM: "+this.currentMem)),this._startTime=i,null!=this._raf&&(this._raf=n.callMethod(window,this.RAF,[e(this,this._tick)]))},_createDiv:function(t,i){null==i&&(i=0);var e;switch((e=window.document.createElement("div")).id=t,e.className=t,e.style.position="absolute",this._pos){case"TL":e.style.left=this._offset+"px",e.style.top=i+"px";break;case"TR":e.style.right=this._offset+"px",e.style.top=i+"px";break;case"BL":e.style.left=this._offset+"px",e.style.bottom=(this._memCheck?48:32)-i+"px";break;case"BR":e.style.right=this._offset+"px",e.style.bottom=(this._memCheck?48:32)-i+"px"}return e.style.width="80px",e.style.height="12px",e.style.lineHeight="12px",e.style.padding="2px",e.style.fontFamily=s.FONT_FAMILY,e.style.fontSize="9px",e.style.fontWeight="bold",e.style.textAlign="center",window.document.body.appendChild(e),e},_createFpsDom:function(){this.fps=this._createDiv("fps"),this.fps.style.backgroundColor=s.FPS_BG_CLR,this.fps.style.zIndex="995",this.fps.style.color=s.FPS_TXT_CLR,this.fps.innerHTML="FPS: 0"},_createMsDom:function(){this.ms=this._createDiv("ms",16),this.ms.style.backgroundColor=s.MS_BG_CLR,this.ms.style.zIndex="996",this.ms.style.color=s.MS_TXT_CLR,this.ms.innerHTML="MS: 0"},_createMemoryDom:function(){this.memory=this._createDiv("memory",32),this.memory.style.backgroundColor=s.MEM_BG_CLR,this.memory.style.color=s.MEM_TXT_CLR,this.memory.style.zIndex="997",this.memory.innerHTML="MEM: 0"},_getFormattedSize:function(t,i){if(null==i&&(i=0),0==t)return"0";var e=Math.pow(10,i),s=Math.floor(Math.log(t)/Math.log(1024));return Math.round(t*e/Math.pow(1024,s))/e+" "+["Bytes","KB","MB","GB","TB"][s]},addInfo:function(t){this.info=this._createDiv("info",this._memCheck?48:32),this.info.style.backgroundColor=s.INFO_BG_CLR,this.info.style.color=s.INFO_TXT_CLR,this.info.style.zIndex="998",this.info.innerHTML=t},clearInfo:function(){null!=this.info&&(window.document.body.removeChild(this.info),this.info=null)},destroy:function(){n.callMethod(window,this.CAF,[this._raf]),this._memoryObj=this._perfObj=this._raf=null,null!=this.fps&&(window.document.body.removeChild(this.fps),this.fps=null),null!=this.ms&&(window.document.body.removeChild(this.ms),this.ms=null),null!=this.memory&&(window.document.body.removeChild(this.memory),this.memory=null),this.clearInfo(),this.currentFps=60,this.currentMs=0,this.currentMem="0",this.avgFps=this.lowFps=60,this._ticks=this._time=this._totalFps=this._measureCount=0,this._fpsMax=this._fpsMin=60,null!=this._perfObj&&null!=(o=this._perfObj,e(o,o.now))?this._startTime=this._perfObj.now():this._startTime=(new Date).getTime(),this._prevTime=-s.MEASUREMENT_INTERVAL},_cancelRAF:function(){n.callMethod(window,this.CAF,[this._raf]),this._raf=null}};var n=function(){};n.field=function(t,i){try{return t[i]}catch(t){return null}},n.callMethod=function(t,i,e){return i.apply(t,e)};var o,h=0;s.MEASUREMENT_INTERVAL=1e3,s.FONT_FAMILY="Helvetica,Arial",s.FPS_BG_CLR="#00FF00",s.FPS_WARN_BG_CLR="#FF8000",s.FPS_PROB_BG_CLR="#FF0000",s.MS_BG_CLR="#FFFF00",s.MEM_BG_CLR="#086A87",s.INFO_BG_CLR="#00FFFF",s.FPS_TXT_CLR="#000000",s.MS_TXT_CLR="#000000",s.MEM_TXT_CLR="#FFFFFF",s.INFO_TXT_CLR="#000000",s.TOP_LEFT="TL",s.TOP_RIGHT="TR",s.BOTTOM_LEFT="BL",s.BOTTOM_RIGHT="BR",s.DELAY_TIME=4e3}("undefined"!=typeof console&&console);new Perf(Perf.TOP_RIGHT,0);document.head.appendChild(t)}(); 
関連する問題