目前我在firefox 47上构建了一个webextension。
当您点击webextension按钮时,会出现一个下拉菜单,从那里我可以导航到其他网站。
出现的按钮是我在manifest.json
中指定的图标。我想知道是否有办法将扩展图标更改为包含我当前访问的URL的文本字符串,并且每次访问新URL时都会更改。
答案 0 :(得分:2)
不,您无法直接将浏览器操作或页面操作图标更改为一些长文本。你可以:
browserAction.setIcon()
更改图标。这会更改显示的图像。它将始终与正常的图标大小相同。您可以在图标图像中包含非常有限的文本。但是,动态执行此操作会很复杂,但在有限集之间切换可能不会那么糟糕。browserAction.setBadgeText()
更改徽章文字。文本数量限制为大约四个字符。browserAction.setBadgeBackgroundColor()
browserAction.setTitle()
更改图标的标题。这显示为工具提示。该文本的长度本身并不受限制。但是,实际向用户显示的内容存在合理的限制。browserAction.onClicked
监听器收听的后台页面,通过提供弹出式网址browserAction.setPopup()
来显示HTML弹出窗口。您可以通过将''
指定为弹出窗口的URL来更改回点击事件。browserAction.enable()
和browserAction.disable()
启用或停用按钮。pageAction.setIcon()
更改图标。这会更改显示的图像。它将始终与正常的图标大小相同。您可以在图标图像中包含非常有限的文本。但是,动态执行此操作会很复杂,但在有限集之间切换可能不会那么糟糕。pageAction.setTitle()
更改图标的标题。这显示为工具提示。该文本的长度本身并不受限制。但是,实际向用户显示的内容存在合理的限制。pageAction.onClicked
监听器收听的后台页面,通过提供弹出式网址pageAction.setPopup()
来显示HTML弹出窗口。您可以通过将''
指定为弹出窗口的URL来更改回点击事件。pageAction.show()
和pageAction.hide()
显示或隐藏按钮。用于创建按钮开/关状态的代码是:
background.js
//The browserButtonStates Object describes the states the button can be in and the
// 'action' function to be called when the button is clicked when in that state.
// In this case, we have two states 'on' and 'off'.
// You could expand this to as many states as you desire.
//icon is a string, or details Object for browserAction.setIcon()
//title must be unique for each state. It is used to track the state.
// It indicates to the user what will happen when the button is clicked.
// In other words, it reflects what the _next_ state is, from the user's
// perspective.
//action is the function to call when the button is clicked in this state.
var browserButtonStates = {
defaultState: 'off',
on: {
//icon : '/ui/is-on.png'
badgeText : 'On',
badgeColor : 'green',
title : 'Turn Off',
action : function(tab) {
chrome.webNavigation.onCommitted.removeListener(onTabLoad);
},
nextState : 'off'
},
off: {
//icon : '/ui/is-off.png'
badgeText : 'Off',
badgeColor : 'red',
title : 'Turn On',
action : function(tab) {
chrome.webNavigation.onCommitted.addListener(onTabLoad);
},
nextState : 'on'
}
}
//This moves the Browser Action button between states and executes the action
// when the button is clicked. With two states, this toggles between them.
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.browserAction.getTitle({tabId:tab.id},function(title){
//After checking for errors, the title is used to determine
// if this is going to turn On, or Off.
if(chrome.runtime.lastError){
console.log('browserAction:getTitle: Encountered an error: '
+ chrome.runtime.lastError);
return;
}
//Check to see if the current button title matches a button state
let newState = browserButtonStates.defaultState;
Object.keys(browserButtonStates).some(key=> {
if(key === 'defaultState') {
return false;
}
let state = browserButtonStates[key];
if(title === state.title) {
newState = state.nextState;
setBrowserActionButton(browserButtonStates[newState]);
if(typeof state.action === 'function') {
//Do the action of the matching state
state.action(tab);
}
//Stop looking
return true;
}
});
setBrowserActionButton(browserButtonStates[newState]);
});
});
function setBrowserActionButton(tabId,details){
if(typeof tabId === 'object' && tabId !== null){
//If the tabId parameter is an object, then no tabId was passed.
details = tabId;
tabId = null;
}
let icon = details.icon;
let title = details.title;
let text = details.badgeText;
let color = details.badgeColor;
//Supplying a tabId is optional. If not provided, changes are to all tabs.
let tabIdObject = {};
if(tabId !== null && typeof tabId !== 'undefined'){
tabIdObject.tabId = tabId;
}
if(typeof icon === 'string'){
//Assume a string is the path to a file
// If not a string, then it needs to be a full Object as is to be passed to
// setIcon().
icon = {path:icon};
}
if(icon) {
Object.assign(icon,tabIdObject);
chrome.browserAction.setIcon(icon);
}
if(title) {
let detailsObject = {title};
Object.assign(detailsObject,tabIdObject);
chrome.browserAction.setTitle(detailsObject);
}
if(text) {
let detailsObject = {text};
Object.assign(detailsObject,tabIdObject);
chrome.browserAction.setBadgeText(detailsObject);
}
if(color) {
let detailsObject = {color};
Object.assign(detailsObject,tabIdObject);
chrome.browserAction.setBadgeBackgroundColor(detailsObject);
}
}
//Set the starting button state to the default state
setBrowserActionButton(browserButtonStates[browserButtonStates.defaultState]);
的manifest.json :
{
"description": "Demo Button toggle",
"manifest_version": 2,
"name": "Demo Button toggle",
"version": "0.1",
"background": {
"scripts": [
"background.js"
]
},
"browser_action": {
"default_icon": {
"32": "myIcon.png"
},
"default_title": "Turn On",
"browser_style": true
}
}
此答案中的代码最初发布在我对In a Firefox WebExtension, how I can make a button that looks and acts like a toggle的回答中。