tampermonkey - Tampermonkey 在特定选项卡上运行脚本
问题描述
我正在学习 Tempermonkey 和它的用户脚本。我有一些问题。
我想为所有 firefox 打开的容器运行一个脚本,但是这个脚本必须根据它运行的容器选项卡具有不同的设置。像某些函数中的某些数据和不同的条件。
我尝试使用GM_getTab
它,但由于某些原因它向我发送了一个空对象。
// @grant GM_getTab
GM_getTab(function (e) {
console.log(e); // 'e is empty in console and in debug window either'
});
那么基本上如何使用这个功能来完成我的任务呢?
PS Firefox:便携式 93.0(64 位)
解决方案
天哪,为什么总是这样,只是问了这个问题,然后在接下来的 10 分钟内找到了解决方案:D
所以解决方案是:
(async () => {
const tabObj = await GM.getTab();
// You first have to define ID for all tabs you need, then just comment this part
tabObj.id = Math.random();
GM_saveTab(tabObj); // update the object
// get all stored objects
const tabsDatabase = await GM.getTabs();
const dbL = Object.keys(tabsDatabase).length;
console.log('Script-owned tab:', tabObj); // Current tab
console.log('Script-owned tabs count:', tabsDatabase); // All opened tabs
})();
推荐阅读
- cucumber - 当使用 '~@tag' 时,第一个 Cucumber 版本会显示“支持 '~@tag' 将从 Cucumber 的下一个版本中删除”?
- javascript - 在带有 XMLHttpRequest 的 Firefox 插件中,Referer、Connection 和 Accept-Encoding 被拒绝
- python - DRF 验证 - 当 Model.clean 出错时返回错误 500
- javascript - React 对象属性设置损坏
- mysql - MYSQL根据另一列字段修改列字段
- c# - 仅使用 xml 文件的一部分
- json - 如何使用 linq 从 sql 中获取数据?
- php - 非法混合排序规则 (latin1_swedish_ci,IMPLICIT) 和 (utf8_unicode_ci,COERCIBLE) 用于操作 '='
- sql - 创建一个触发器,将插入中的空值替换为 SQL Server 表中已存在的值
- r - R latex2exp 切换回非下标