我试图注入一个链接到ChatGBT的左侧菜单栏使用Chrome扩展,类似于AIPRM for ChatGBT Chrome扩展正在插入额外的菜单项.

enter image description here

我正在使用以下内容,但不工作.

menifest.json:

{
  "name": "Auto Snippet AI",
  "version": "1.0",
  "description": "Inject a URL called 'https://autosnippet.ai/' to the left navigation bar of 'https://chat.openai.com/'",
  "manifest_version": 3,
  "permissions": [
    "activeTab",
    "tabs"
  ],
  "icons": {
    "16": "icon.png",
    "32": "icon.png",
    "48": "icon.png",
    "128": "icon.png"
  },
  "content_scripts": [
    {
      "js": [
        "content.js"
      ],
      "matches": [
        "https://chat.openai.com/*"
      ]
    }
  ]
}

content.js:

document.addEventListener('DOMContentLoaded', function () {
    // Find the nav div
    const nav = document.querySelector("#__next > div.overflow-hidden.w-full.h-full.relative.flex > div.dark.hidden.bg-gray-900.md\\:flex.md\\:w-\\[260px\\].md\\:flex-col > div > div > nav");

    // Check if the nav div exists
    if (nav) {
        // Create a new link for the Auto Snippet AI website
        const a = document.createElement('a');
        a.classList.add('nav-item');
        a.href = 'https://autosnippet.ai/';
        a.target = '_blank';
        a.innerText = 'Auto Snippet AI';

        // Insert the link into the nav div
        nav.insertBefore(a, nav.lastChild);
    }
}); 

所以,我想注入一个链接"自动代码片断AI"在左侧菜单栏类似于AIPRM是注入一个链接"AIPRM for SEO Powered"时安装.任何建议和帮助都将受到高度赞赏.

推荐答案

看起来DOMContentLoaded 事件没有启动. https://stackoverflow.com/a/39993724/9814969

或者,您可以在 list .json中添加"run_at" : "document_end"

...
  "content_scripts": [
    {
      "js": ["content.js"],
      "matches": ["https://chat.openai.com/*"],
      "run_at": "document_end"
    }
  ]
...

因此,没有addEventListener的Content.js将如下所示

const nav = document.querySelector(
    "#__next > div.overflow-hidden.w-full.h-full.relative.flex > div.dark.hidden.bg-gray-900.md\\:flex.md\\:w-\\[260px\\].md\\:flex-col > div > div > nav"
  );

  // Check if the nav div exists
  if (nav) {
    // Create a new link for the Auto Snippet AI website
    const a = document.createElement("a");
    a.classList.add("nav-item");
    a.href = "https://autosnippet.ai/";
    a.target = "_blank";
    a.innerText = "Auto Snippet AI";

    // Insert the link into the nav div
    nav.insertBefore(a, nav.lastChild);
  }

Javascript相关问答推荐

具有相同参数的JS类

使用useup时,React-Redux无法找到Redux上下文值

如何在使用fast-xml-parser构建ML时包括属性值?

嵌套异步JavaScript(微任务和macrotask队列)

无法检测卡片重叠状态的问题

并不是所有的iframe都被更换

为什么我的导航条打开状态没有在窗口addeventlistener(Reaction Js)中更新?

如何在文本字段中输入变量?

在Java中寻找三次Bezier曲线上的点及其Angular

如何将数组用作复合函数参数?

Webpack在导入前混淆文件名

按什么顺序接收`storage`事件?

ngOnChanges仅在第二次调用时才触发

按特定顺序将4个数组组合在一起,按ID分组

未找到用于 Select 器的元素:in( puppeteer 师错误)

我的NavLink活动类在REACT-ROUTER-V6中出现问题

重新渲染过多(&Q).REACT限制渲染次数以防止无限循环.使用REACT下拉菜单时

如何调整下拉内容,使其不与其他元素重叠?

Reaction路由v6.4+数据API:重试加载程序而不显示错误

如何在不获取其子元素的文本内容的情况下获取元素的文本内容?