如何使用Chrome插件/扩展程序将链接注入到ChatGPT左侧菜单栏。

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

How to inject a link to ChatGPT left menu bar using chrome plugin/extension

问题

I am trying to inject a link to ChatGPT left menu bar using chrome extension similar to AIPRM for ChatGPT chrome extension is inserting additional menu items.

enter image description here

I am using the following but not working.

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);
    }
}); 

So, I want to inject a link "Auto Snippet AI" in left menu bar similar to AIPRM is injecting a link "AIPRM for SEO powered" when installed. Any suggestion and help will be highly appreciable.

英文:

I am trying to inject a link to ChatGPT left menu bar using chrome extension similar to AIPRM for ChatGPT chrome extension is inserting additional menu items.

enter image description here

I am using the following but not working.

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);
    }
}); 

So, I want to inject a link "Auto Snippet AI" in left menu bar similar to AIPRM is injecting a link "AIPRM for SEO powered" when installed. Any suggestion and help will be highly appreciable.

答案1

得分: 1

看起来 `DOMContentLoaded` 事件没有触发。
https://stackoverflow.com/a/39993724/9814969

或者您可以在 manifest.json 中添加 `"run_at" : "document_end"`,如下所示:

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

这样,content.js 将不需要 addEventListener,并且会如下所示:

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"
  );

  // 检查 nav div 是否存在
  if (nav) {
    // 为 Auto Snippet AI 网站创建新链接
    const a = document.createElement("a");
    a.classList.add("nav-item");
    a.href = "https://autosnippet.ai/"
    a.target = "_blank";
    a.innerText = "Auto Snippet AI";

    // 将链接插入到 nav div 中
    nav.insertBefore(a, nav.lastChild);
  }
英文:

Looks like DOMContentLoaded event isn't firing.
https://stackoverflow.com/a/39993724/9814969

Or you can add "run_at" : "document_end" in the manifest.json

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

so the content.js would look like this without the addEventListener

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);
  }

huangapple
  • 本文由 发表于 2023年4月17日 09:31:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76031163.html
匿名

发表评论

匿名网友

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

确定