英文:
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.
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.
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论