javascript – 我如何制作一个外观和行为类似于切换的浏览器操作按钮
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 我如何制作一个外观和行为类似于切换的浏览器操作按钮,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7255字,纯文字阅读大概需要11分钟。
内容图文
![javascript – 我如何制作一个外观和行为类似于切换的浏览器操作按钮](/upload/InfoBanner/zyjiaocheng/788/81d75c99b0d6488a9803a393ee5de74f.jpg)
目标是获取Firefox的WebExtension,可以由工具栏中的用户激活/停用,如开/关开关.
我正在使用带有此代码的background.js:
browser.browserAction.onClicked.addListener(function (tab) {
switch (button) {
case 'turn-on':
enable();
break;
case 'turn-off':
disable();
break;
}
});
function enable() {
browser.browserAction.setIcon({ path: '/ui/is-on.png', });
browser.browserAction.setPopup({ popup: '/ui/turn-off.js', });
browser.webNavigation.onCommitted.addListener(onTabLoad);
}
function disable() {
browser.browserAction.setIcon({ path: '/ui/is-off.png', });
browser.browserAction.setPopup({ popup: '/ui/turn-on.js', });
browser.webNavigation.onCommitted.removeListener(onTabLoad);
}
function onTabLoad(details) {
browser.tabs.executeScript(details.tabId, {
file: '/gc.js',
allFrames true,
});
}
enable(); // enable or disable by default
显然我做错了什么.我是编码新手.这是我正在努力完成的个人项目.
解决方法:
你的代码
当您添加一个switch语句来打开按钮时,您从未定义按钮,也没有更改其状态.您也没有默认情况,以防按钮变量不是您在case语句中测试的值之一.
您不应该使用browserAction.setPopup()来设置弹出窗口.设置弹出窗口将导致弹出窗口被打开而不是您的后台页面接收到单击事件.此外,弹出窗口必须是HTML页面,而不是JavaScript.
有关在onTabLoad()中需要解决的Firefox错误,请参阅以下部分.
收听webNavigation.onCommitted并不足以涵盖需要注入脚本的所有情况.换句话说,每次加载页面时都不会触发webNavigation.onCommitted.要完全覆盖脚本需要注入的每种情况,您需要在另一个问题中提出要求.
var nextButtonState;
browser.browserAction.onClicked.addListener(function (tab) {
switch (nextButtonState) {
case 'turn-on':
enable();
break;
case 'turn-off':
default:
disable();
break;
}
});
function enable() {
browser.browserAction.setIcon({ path: '/ui/is-on.png', });
//browser.browserAction.setPopup({ popup: '/ui/turn-off.js', });
browser.webNavigation.onCommitted.addListener(onTabLoad);
nextButtonState = 'turn-off';
}
function disable() {
browser.browserAction.setIcon({ path: '/ui/is-off.png', });
//browser.browserAction.setPopup({ popup: '/ui/turn-on.js', });
browser.webNavigation.onCommitted.removeListener(onTabLoad);
nextButtonState = 'turn-on';
}
function onTabLoad(details) {
//Add a setTimout to avoid a Firefox bug that Firefox is not quite ready to
// have tabs.executeScript() inject a script when the onCommitted event fires.
setTimeout(function(){
chrome.tabs.executeScript(details.tabId, {
file: '/gc.js',
allFrames true,
});
},0);
}
enable(); // enable or disable by default
Firefox webNavigation.onCommitted错误的解决方法
您的onTabLoad()代码需要更改才能使用webNavigation.onCommitted侦听器在Firefox中使用tabs.executeScript()注入脚本(Chrome中不需要这样做).这是由于Firefox中的一个错误导致tabs.executeScript()失败,如果立即从webNavigation.onCommitted侦听器执行.我使用的解决方法是在setTimeout(函数,0)延迟后注入脚本.这允许Firefox执行设置executeScript()运行所必需的环境所需的代码.
function onTabLoad(details) {
//Add a setTimout to avoid a Firefox bug that Firefox is not quite ready to
// have tabs.executeScript() inject a script when the onCommitted event fires.
setTimeout(function(){
chrome.tabs.executeScript(details.tabId, {
file: '/gc.js',
allFrames true,
});
},0);
}
多状态按钮的通用解决方案(例如切换按钮)
我用来使浏览器操作按钮的行为类似于切换的代码如下.我修改了browserButtonStates对象,它描述了按钮的作用和它们的外观,以添加和删除webNavigation.onCommitted侦听器onTabLoad().有关onTabLoad()的问题,请参见上文.
下面的代码比您需要的更复杂.我写它的目的是能够将它从一个项目移动到另一个项目,只需要更改browserButtonStates对象的内容.然后,仅通过改变该对象,可以改变在每个状态(例如开/关)中执行的图标,文本,徽章文本,徽章颜色和动作.
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
}
}
内容总结
以上是互联网集市为您收集整理的javascript – 我如何制作一个外观和行为类似于切换的浏览器操作按钮全部内容,希望文章能够帮你解决javascript – 我如何制作一个外观和行为类似于切换的浏览器操作按钮所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。