前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >GM_addValueChangeListener 函数的使用 详解 编写高级扩展浏览器脚本

GM_addValueChangeListener 函数的使用 详解 编写高级扩展浏览器脚本

作者头像
拿我格子衫来
发布2022-01-24 17:55:34
7180
发布2022-01-24 17:55:34
举报
文章被收录于专栏:TopFETopFE

在TM的文档中有这样一个API

GM_addValueChangeListener

GM_addValueChangeListener(name, function(name, old_value, new_value, remote) {})

Adds a change listener to the storage and returns the listener ID. 'name' is the name of the observed variable. The 'remote' argument of the callback function shows whether this value was modified from the instance of another tab (true) or within this script instance (false). Therefore this functionality can be used by scripts of different browser tabs to communicate with each other.

这个API的作用就是可以对存储在GM中的变量进行监听,当值变化时,可以触发一个函数,

name是要监听的变量名称, 字符串类型, name指向的变量必须是基本数据类型,如果是js中的引用类型object是无法触发监听函数的

监听函数,第一个参数是变量名称, 第二个是旧值,第三个是新值, 第四个是表示,值的变化是在当前浏览器窗口还是其他脚本触发的, 其他窗口为true 其他脚本触发的为false

具体用法需要搭配GM_setValue 函数,对变量进行赋值

具体用法如下

代码语言:javascript
复制
// ==UserScript==
// @name         allOpen
// @namespace    https://fizzz.blog.csdn.net/
// @version      0.1
// @description  try to take over the world!
// @author       Fizz
// @grant        GM_setValue
// @grant        GM_addValueChangeListener


// ==/UserScript==

(function () {
  'use strict';
  GM_setValue('globalStatu', 'initial') // open close
  GM_addValueChangeListener('globalStatu', function(name, old_value, new_value, remote) {
    console.log(name, old_value, new_value, remote)
    window.kk="https://fizzz.blog.csdn.net/";
    window.close();
  })

  GM_setValue('globalStatu', 'close')

})

使用GM_addValueChangeListener 可以很简单地编写跨浏览器窗口的脚本. 就是如此简单 强大

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • GM_addValueChangeListener(name, function(name, old_value, new_value, remote) {})
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档