如何在数组内一个元素引用另一个元素?

qwr124 2021-01-30 04:54:36
我想将每个图标打开的次数分别存储在油猴里面,建了一个数组分别存放图标的名称、图片、链接、弹出的信息,弹出的信息是一个函数,点击图标时执行打开链接和统计次数

我需要在函数里面引用当前的图标的名称,如何通过 JavaScript 实现呢?或者是否有其他的办法?

在这个脚本基础上改的 https://greasyfork.org/zh-CN/scripts/404859


var iconArray = [
{
name: 'Google',
image: 'https://i.ibb.co/R9HMTyR/1-5.png',
host: ['www.google.com'],
popup: function (text, name) {
open('https://www.google.com/s?wd=' + encodeURIComponent(text), name);
console.log(name);
}
},
{
name: 'Bing',
image: 'https://i.ibb.co/R9HMTyR/1-5.png',
host: ['www.bing.com'],
popup: function (text, name) {
open('https://www.bing.com/s?wd=' + encodeURIComponent(text), name);
console.log(name);
}
},
]




function open(url, a) {
try {
if(GM_openInTab(url, { loadInBackground: true, insert: true, setParent :true })){

if(GM_getValue(a).times){
GM_setValue(a, {
'times': GM_getValue(a).times + 1
});
}else{
GM_setValue(a, {
'times': 1
});
}
console.log('times-'+GM_getValue(a).times);

} else{

}
} catch (error) {
return GM_openInTab(url, { loadInBackground: true, insert: true, setParent :true });
}
}

我想在油猴里面这样存放数据,打开 Google 1 次,Bing 4 次

{
"Google": {
"times": "1",
},
"Bing": {
"times": "4",
},

}
...全文
161 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
qwr124 2021-01-31
  • 打赏
  • 举报
回复
通过调用 popup 的时候传入 name 解决啦

87,996

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧