如何使用Chrome扩展模仿调试控制台的功能。

huangapple go评论71阅读模式
英文:

How to mimic the capability of the debug console with a chrome extension

问题

我正在尝试开发一个Chrome扩展,用于Desmos。我的扩展需要一种方法来更新左侧的方程单元格。手动执行此操作的一种方法是打开Chrome开发者工具控制台,并与预定义变量Calc进行交互(尝试Calc.setExpression({"id": "0", "latex": "x = y"}))。理想情况下,我想要在我的Chrome扩展中使用类似的功能,但我还没有找到如何实现的方法。我尝试查看了API,但它只在将Desmos图嵌入您自己的网站时才有用。我应该如何做到这一点?

英文:

I am trying to develop a chrome extension for Desmos. My extension needs a way to update the equation cells on the left. One way to do this manually is to open the chrome devtools console and interface with a predefined variable Calc (try Calc.setExpression({"id": "0", "latex": "x = y"})). Ideally, I want to have some sort of a similar functionality that I can use in my chrome extension but I have not found a way to do so. I tried looking through the api, but it is only useful if you are embedding Desmos graphs in your own website. How should I do this?

答案1

得分: 1

以下是您提供的内容的中文翻译:

manifest.json

{
  "manifest_version": 3,
  "name": "hoge",
  "version": "1.0",
  "action": {
    "default_popup": "popup.html"
  },
  "permissions": [
    "scripting"
  ],
  "host_permissions": [
    "<all_urls>"
  ]
}

popup.html

<html>
<body>
  <script src="popup.js"></script>
</body>
</html>

popup.js

hoge = () => {
  Calc.setExpression({ "id": "0", "latex": "x = y" })
}

chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
  chrome.scripting.executeScript(
    {
      target: { tabId: tabs[0].id },
      world: "MAIN",
      func: hoge
    }
  );
});
英文:

Here is a sample of it.

manifest.json

{
  &quot;manifest_version&quot;: 3,
  &quot;name&quot;: &quot;hoge&quot;,
  &quot;version&quot;: &quot;1.0&quot;,
  &quot;action&quot;: {
    &quot;default_popup&quot;: &quot;popup.html&quot;
  },
  &quot;permissions&quot;: [
    &quot;scripting&quot;
  ],
  &quot;host_permissions&quot;: [
    &quot;&lt;all_urls&gt;&quot;
  ]
}

popup.html

&lt;html&gt;
&lt;body&gt;
  &lt;script src=&quot;popup.js&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

popup.js

hoge = () =&gt; {
  Calc.setExpression({ &quot;id&quot;: &quot;0&quot;, &quot;latex&quot;: &quot;x = y&quot; })
}

chrome.tabs.query({ active: true, currentWindow: true }, (tabs) =&gt; {
  chrome.scripting.executeScript(
    {
      target: { tabId: tabs[0].id },
      world: &quot;MAIN&quot;,
      func: hoge
    }
  );
});

huangapple
  • 本文由 发表于 2023年5月7日 15:14:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定