javascript - 在 google chrome 地址栏中执行 JavaScript 函数

标签 javascript google-chrome address-bar

我想在我的 google chrome javascript: 的地址栏中执行 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 代码,例如上面的代码?

最佳答案

地址栏似乎不接受多行代码。因此,当我通过一个缩小器运行我的代码并将其全部放在一条线上时,它起作用了。例如,以下代码将在您页面的右上角放置一个 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)}();

关于javascript - 在 google chrome 地址栏中执行 JavaScript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45398892/

相关文章:

javascript - 如何使用 razor 使这个 javascript 更加动态?

javascript - Angular 类和 ng 类

javascript - GWT:隐藏移动设备上的地址栏

google-chrome-extension - Chrome 扩展程序 : blank new tab + address bar focus

javascript - Angular2 [ngClass] 条件引导样式被切断

javascript - 无法识别传单上的颜色

javascript - Chrome 中的 DOM 操作速度很慢(隐藏/显示元素)

css - Chrome v52 html5 视频变长了吗?

css - 通过 :hover:before causes base element to disappear on mouse out 添加内容

google-chrome-extension - Google Chrome 扩展程序 - 如何从热门网站列表中删除条目(多功能框建议)?