利用多页面共享的脚本,
能够实现利用油猴跨域共享页面数据。
// ==UserScript==// @name 跨页面数据传输脚本// @namespace http://tampermonkey.net/// @version 0.1// @description 利用共用脚本实现跨域通信// @connect *// @author dzt// @grant GM_getValue// @grant GM_setValue// @grant GM_removeValueChangeListener// @grant GM_addValueChangeListener// @run-at document-end// @match 页面A// @include 页面B// @include 页面C// ==/UserScript==(function() { 'use strict'; unsafeWindow.__t_sendCrossDomainMessage= function(data){ let __source = location.href; let __domain = document.domain; //利用createObjectURL生成不反复的uuid const __url = URL.createObjectURL(new Blob()); const uuid = __url.substring(__url.lastIndexOf('/') + 1); console.log('生成新的crossDomainMessage-uuid:',uuid); GM_setValue('data',data); GM_setValue('source',__source); GM_setValue('domain',__domain); GM_setValue('uuid',uuid);//触发数据变更 } //ValueChangeListener须要应用值类型的变动来触发,这里应用了uuid字符串触发 //fn可选退出数据源判断 unsafeWindow.__t_onReceiveCrossDomainMessage = function(fn){ GM_removeValueChangeListener('uuid'); GM_addValueChangeListener('uuid',(name,oldValue,newValue,remote)=>{ //console.log(name,oldValue,newValue,remote); console.log('crossDomainMessage-uuid 已扭转','crossDomainMessage-uuid is Change!!'); let uuid = newValue; let data = GM_getValue('data'); let source = GM_getValue('source'); let domain = GM_getValue('domain'); return fn({uuid,data,source,domain,source}); }) } // Your code here...})();