admin管理员组文章数量:1333210
I want to execute JavaScript code in address bar of my google chrome javascript:
. It works fine with something simple such as:
javascript:alert("aaa");
But it doesn't work with more than one alert. I found out that placing everything in brackets would do the trick:
javascript:{alert("aaa"); alert("blabla"); document.getElementsById("myId").innerHTML = "aa";}
But it doesn't work with something more plex such as for
or calling a function. I really need to execute for
like this:
var elements = document.getElementsByTagName('div');
for (var i = 0; i < elements.length; i++) {
elements[i].innerHTML = "foo";
}
I tried it with both javascript:
and javascript:{
...}
and it didn't work.
So my question is: How can I execute more plex JavaScript code such as this one above?
I want to execute JavaScript code in address bar of my google chrome javascript:
. It works fine with something simple such as:
javascript:alert("aaa");
But it doesn't work with more than one alert. I found out that placing everything in brackets would do the trick:
javascript:{alert("aaa"); alert("blabla"); document.getElementsById("myId").innerHTML = "aa";}
But it doesn't work with something more plex such as for
or calling a function. I really need to execute for
like this:
var elements = document.getElementsByTagName('div');
for (var i = 0; i < elements.length; i++) {
elements[i].innerHTML = "foo";
}
I tried it with both javascript:
and javascript:{
...}
and it didn't work.
So my question is: How can I execute more plex JavaScript code such as this one above?
Share Improve this question edited Jul 30, 2017 at 11:58 Nisarg Shah 14.6k6 gold badges38 silver badges57 bronze badges asked Jul 30, 2017 at 10:35 user8388991user8388991 1- Run your code through a minify tool like jspress. and it will work even with "javascript:" – Buzzy Commented Jul 30, 2017 at 12:03
3 Answers
Reset to default 2Seems like the URL bar doesn't accept multi-line code. So when I ran mine through a minifier to put it all on one line, it worked. For example, the following code will put an FPS counter (frames per second) at the top right of your page:
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)}();
The reason your code is not working is because it has a valid return value. So when you run a statement such as elements[i].innerHTML = "foo"
, it returns the value foo
. So the browser does update the DOM, but it also need to show the oute of the statement. As a result the entire page is immediately replaced by the returned value.
The solution of placing a void(0)
as suggested in @Djordje's answer works only because the return value of void(0)
is undefined
. Thus the browser no longer needs to show that value and you can see the updated DOM.
Before I noticed the answer, I was trying to run the following code. Which also works because its return value is also undefined
.
{
var elements = document.getElementsByTagName('div');
for (var i = 0; i < elements.length; i++) {
elements[i].innerHTML = "foo";
}
undefined;
}
To see this effect better, you could add an alert statement just after the DOM updates, and then return a valid value. Like this:
{
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.";
}
You can warp everything inside an immediately-invoked Function Expressions (IIFE):
javascript:(function(){
var elements = document.getElementsByTagName('div');
for (var i = 0; i < elements.length; i++) {
elements[i].innerHTML = "foo";
}})()
I kept the newlines to make the code clearer. you might need to write everything in a single line.
本文标签: Execute JavaScript functions in google chrome address barStack Overflow
版权声明:本文标题:Execute JavaScript functions in google chrome address bar - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742285373a2446832.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论