如何在Chrome中禁用调试器关键字或禁用暂停

3
我知道有一个“永不暂停”选项,但对于这样的代码它不起作用。
setInterval(function(){
  eval('debugger;'+Math.random())
},1000)

如果我找不到setInterval,我就无法禁用暂停,这非常令人恼火。
是否有任何标志或方法可以禁用此功能?
编辑
我发现这个问题与此问题(DevTools:无法禁用由“debugger”语句引起的断点)有关,在测试代码中,我找到了一个标志--expose-debug-as debug,但我如何在无头模式下使用此标志?
chrome --expose-debug-as debug --headless --disable-gpu '<URL>' --repl
[0610/020053.677043:ERROR:headless_shell.cc(459)] Open multiple tabs is only supported when the remote debug port is set.

嗯,删掉这段代码吧.... ;) - epascarello
@epascarello 那不是一个选项,那不是我的工作。 - wener
3个回答

4

那么你唯一的选择就是注入代码到页面中,覆盖并删除eval函数。

(function () {
  var _eval = window.eval;
  window.eval = function (str) {
    _eval(str.replace(/debugger;/,""));
  };
}());
eval("debugger;alert('a');")

听起来不错,我也试图清除所有间隔,eval替换可能会导致脚本破损,因为一些编译器使用eval加密代码。 - wener
请注意,覆盖eval将破坏在其内部范围之外创建变量的代码,即在被调用者范围内 - 只有内置的eval可以做到这一点。 - wOxxOm

0

我通过禁用间隔和超时来完成这个操作,有些网站使用这种方法来防止他人查看代码。

我使用 Chrome Headless 在 onload 之前插入了代码。

启动 Headless。

chrome --headless --disable-gpu <URL> --remote-debugging-port=9222

其他的 shell 会话

yarn add chrome-remote-interface

test.es6

const CDP = require('chrome-remote-interface');
async function test() {
  const protocol = await CDP({port: 9222});
  // Extract the DevTools protocol domains we need and enable them.
  // See API docs: https://chromedevtools.github.io/devtools-protocol/
  const {Page, Runtime, Debugger, Log, Console} = protocol;
  try {
      await Promise.all([
          Page.enable(),
          Runtime.enable(),
          Log.enable(),
          Console.enable(),
          Debugger.disable(),
      ]);
  } catch (e) {
      console.log('Failed', e)
      return
  }

  Log.entryAdded(({entry: e}) =>
      console.log(`${new Date(e.timestamp).toISOString()} ${e.source}:${e.level} ${e.text}`)
  );
  Console.messageAdded(({message: e}) =>
      console.log(`${new Date().toISOString()} ${e.source}:${e.level} ${e.text}`)
  )
  Page.navigate({url: URL_HERE});
  // Inject code,disable setInterval and setTimeout
  Runtime.executionContextCreated(async ({context}) => {
      console.log('executionContextCreated')
      let result = await Runtime.evaluate({
          expression: `
          window._si=window.setInterval
          window.setInterval=(...args)=>{
              let id = 1//window._si.apply(window,args)
              console.warn(\`setInterval:\${args}\`,)
              return id
          }

          window._st=window.setTimeout
          window.setTimeout=(...args)=>{
              let id = 1//window._st.apply(window,args)
              console.warn(\`setTimeout:\${args}\`,)
              return id
          }
          ;location.href
          `,
          contextId: context.id,
      });
      console.log('executionContextCreated', result)
  });
  // Wait for window.onload before doing stuff.
  Page.loadEventFired(async () => {

      // Debugger.setSkipAllPauses(true)
      const js = `
      console.log('Page load');
      document.querySelector('title').textContent
      `;
      // Evaluate the JS expression in the page.
      const result = await Runtime.evaluate({expression: js});
      console.log('Title of page: ' + result.result.value);
      protocol.close();
  });
}
test()

在脚本运行后,在Chrome中打开“localhost:9222”,检查页面,调试器现在不会运行。

0
如果您不需要 setInterval 做其他事情,只需在控制台中键入以下内容:
window.setTimeout = null

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接