My goal is to write a browser extension, that can rename the titles of multiple tabs at once. Or to be more precise: append a string at the start of each title.
Do you have any ideas, how that could be achieved?
Here are some of my attempts so far:
This code would allow me(with the tabs permission enabled and without the activeTab permission) to get the tabs objects of all the tabs of the current window:
chrome.tabs && chrome.tabs.query({
currentWindow: true
}, tabs => {
for (let i = 0; i < tabs.length; i++) {
chrome.tabs.sendMessage(
tabs[i].id || 0,
{}
)
}
});
for each "tabs" object (which contains a tabs.title) I could perhaps use the sendMessage function? That was the initial idea. But I'm not sure how I would do that or if that even makes sense.
In general, I'm wondering if the title of each tab needs to be changed by setting document.title.
That could potentially be done via a script. I have seen this solution elsewhere:
chrome.tabs.executeScript({
code: 'document.title=' + JSON.stringify(newTitle)
});
But I also need to specify for which tab the document.title needs to be changed.
And I'm using Manifest version 3. And the executeScript function was deprecated. Instead there is now a scripting api with it's own permission. It contains an executeScript function:
chrome.scripting.executeScript(
injection: ScriptInjection,
callback?: function,
)
whereby the ScriptInjection has a target.
Which I could set to tabs[i].id inside the for loop shown above.
Perhaps like this?:
chrome.tabs && chrome.tabs.query({
currentWindow: true
}, tabs => {
for (let i = 0; i < tabs.length; i++) {
chrome.scripting.executeScript(
injection: () => {target: { tabId: tabs[i].id }}, files: [renameTitleScript.js]}
)
}
});
I don't like that I need a separate file with a script. Instead of just having a function in the same scope that lets me pass the new title. And since I don't know whether I'm even on the correct path, I'm hoping you can give me some suggestions.