【问题标题】:Can I access the console Command line API (like $$ and traceAll from Firebug or Chrome Inspector Console) from an external javascript?我可以从外部 javascript 访问控制台命令行 API(例如来自 Firebug 或 Chrome Inspector 控制台的 $$ 和 traceAll)吗?
【发布时间】:2012-11-28 01:22:47
【问题描述】:

是否可以从外部 api 访问Command Line Api

简单例子:

HTML

  <div id="myDiv"></div>
  <script src="myScript.js"></script>

myScript.js

$$('#myDiv').textContent = 'this will not work';

我不想加载像 jQuery 或 Zepto 这样的外部库,因为这样的视图已经在本地加载了。

【问题讨论】:

  • 如果命令行在某人的浏览器中不可用怎么办?这些命令将失败。我认为他们分开是有原因的
  • 如果存在这种能力,那将是一个巨大的安全漏洞,因为输入命令行 API 的命令会被粘贴到页面中,并使用 eval() 进行评估。这将允许恶意站点运行脚本注入并获得对浏览器会话信息的访问权限。

标签: javascript api console command firebug


【解决方案1】:

回答你的问题,不。但我不认为你真的想要。 API 可能会更改,从而破坏您的代码。如果您要查找的只是查询选择器。我认为你最好使用 MDN 上的 sn-p。

function $ (selector, el) {
    if (!el) {el = document;}
    return el.querySelector(selector);
}
function $$ (selector, el) {
    if (!el) {el = document;}
    return el.querySelectorAll(selector);
    // Note: the returned object is a NodeList.
    // If you'd like to convert it to a Array for convenience, use this instead:
    // return Array.prototype.slice.call(el.querySelectorAll(selector));
}
alert($('#myID').id);

Document.querySelector

【讨论】:

    猜你喜欢
    • 2011-10-26
    • 2013-04-16
    • 1970-01-01
    • 2014-12-17
    • 1970-01-01
    • 2015-06-11
    • 2017-10-05
    • 1970-01-01
    • 2016-10-21
    相关资源
    最近更新 更多