英文:
Chrome/Edge extension with current time as tab-title
问题
如何开发一个 Chrome/Edge 扩展,始终在标签标题中显示当前时间,位于实际标题之前?
英文:
How can I develop a Chrome/Edge extension that always displays the current time in the tab title before the actual title?
答案1
得分: 1
你可以使用 chrome.tabs
API 获取当前选项卡并用当前时间更新其标题。
这段代码监听 tabs.onUpdated
事件,当选项卡更新时触发(例如页面加载完成时)。触发此事件时,将调用 updateTabTitle
函数,并传入更新选项卡的 tabId
。该函数获取当前时间,构建一个新的标题字符串,其中包含当前时间和原始选项卡标题,并使用 chrome.tabs.executeScript
更新选项卡的标题。
请注意,此代码仅在页面加载完成时更新选项卡标题。如果要更频繁地更新标题(例如,每秒钟),您可以使用 setInterval
定期调用 updateTabTitle
。
还请注意,在清单 v3 中,chrome.tabs.executeScript
方法需要在清单文件中明确授予 scripting
权限。以下是如何在清单中包含此权限的示例:
{
"name": "My Extension",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
},
"permissions": [
"tabs",
"scripting"
]
}
有关 chrome.tabs
API 和 scripting
权限的更多信息,请参见以下链接:
英文:
you can use the chrome.tabs
API to get the current tab and update its title with the current time.
Here's a sample code snippet that you can use as a starting point:
// background.js
function updateTabTitle(tabId) {
  chrome.tabs.get(tabId, function(tab) {
    var currentTime = new Date().toLocaleTimeString();
    var newTitle = currentTime + " - " + tab.title;
    chrome.tabs.executeScript(tabId, {code: "document.title = '" + newTitle + "';"});
  });
}
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
  if (changeInfo.status == "complete") {
    updateTabTitle(tabId);
  }
});
This code listens for the tabs.onUpdated
event, which is fired when a tab is updated (e.g. when a page finishes loading). When this event is fired, the updateTabTitle
function is called with the tabId
of the updated tab. This function gets the current time, constructs a new title string with the current time and the original tab title, and updates the tab's title using chrome.tabs.executeScript
.
Note that this code only updates the tab title when the page finishes loading. If you want to update the title more frequently (e.g. every second), you can use setInterval
to call updateTabTitle
at regular intervals.
Also note that in manifest v3, the chrome.tabs.executeScript
method requires the scripting
permission to be explicitly granted in the manifest file. Here's an example of how to include this permission in your manifest:
{
  "name": "My Extension",
  "version": "1.0",
  "manifest_version": 3,
  "background": {
    "service_worker": "background.js"
  },
  "permissions": [
    "tabs",
    "scripting"
  ]
}
For more information on the chrome.tabs
API and the scripting
permission, see the following links:
答案2
得分: 0
1.) 创建新文件夹 "time2tab"。
2.) 将新文件 "manifest.js" 放入其中:
{
"manifest_version": 3,
"name": "time2tab",
"version": "1.0",
"description": "一个简单的Chrome扩展,将当前标签的标题更改为当前时间戳。",
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"content.js"
]
}
]
}
3.) 将新文件 "content.js" 放入其中:
var originalTitel = document.title;
function updateTime() {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var tenthOfASecond = Math.floor(now.getMilliseconds() / 100);
var timeString = hours.toString().padStart(2, '0') + ':' + minutes.toString().padStart(2, '0') + ':' + seconds.toString().padStart(2, '0') + '.' + tenthOfASecond.toString();
document.title = timeString + " " + originalTitel;
}
setInterval(updateTime, 100);
4.) 在扩展选项卡中激活开发者模式。通过“加载未打包的扩展”加载文件夹。
英文:
1.) Create new folder "time2tab".
2.) Put new file "manifest.js" in it:
{
"manifest_version": 3,
"name": "time2tab",
"version": "1.0",
"description": "A simple Chrome extension that changes the title of the current tab to the current timestamp.",
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"content.js"
]
}
]
}
3.) Put new file "content.js" in it:
var originalTitel = document.title;
function updateTime() {
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var seconds = now.getSeconds();
var tenthOfASecond = Math.floor(now.getMilliseconds() / 100);
var timeString = hours.toString().padStart(2, '0') + ':' + minutes.toString().padStart(2, '0') + ':' + seconds.toString().padStart(2, '0') + '.' + tenthOfASecond.toString();
document.title = timeString + " " + originalTitel;
}
setInterval(updateTime, 100);
4.) Aktivate Developer mode in Extensions-Tab. Load the folder via Load unpacked.
Use cases for this extension:
- This extension can be used as a minimal working example for developing a Chrome/Edge extension.
- It can also be used to record the current time in a screencast recording an automated test.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论