2016-11-13 113 views
3

使用localStorage有一種方法transfer data between pages on the same domain,但我需要在不同域之間傳輸數據。在Chrome中,我嘗試使用符號鏈接,以便域共享存儲,但在其他域中找不到設置在一個域中的項目,直到我重新啓動Chrome。如何在不同域的用戶腳本之間傳輸數據?我將使用任何可用的瀏覽器。在不同域上的腳本之間傳輸數據

這將只能用於個人用途

+0

我不知道的,可以解決這個問題爲您帶來任何瀏覽器提供的方式,也有一些很好的理由,這將是一件壞事(安全性,隱私,存儲限制,安全!保密!)。然而,我想如果你熱衷於解決這個問題,並且不關心你自己的用戶腳本的這些東西,你可以將數據發佈到不同的資源。例如你在'website-a'和'website-b'上運行userscript,並使用AJAX他們從/到'website-c'獲取/發佈數據。如果這是你想與其他人分享的東西,那麼對於個人資料來說,再好不過了,情況會變得更加複雜。 – Dymos

回答

1

我會用@include包括兩個域,然後使用GM_getValueGM_setValue來存儲和檢索數據。

我還介紹瞭如何使用GM_registerMenuCommand函數的示例,該函數在用戶從userscript插件彈出窗口中選擇選項時打開提示。

// ==UserScript== 
// @name   Pinky 
// @namespace http://pinkyAndTheBrain.net/ 
// @version  0.1 
// @description try to take over the world! 
// @author  You 
// @include  https://domain1.com 
// @include  https://domain2.com 
// @grant  GM_getValue 
// @grant  GM_setValue 
// @grant  GM_registerMenuCommand 
// ==/UserScript== 
/* global GM_getValue, GM_setValue, GM_registerMenuCommand */ 
/* jshint esnext:true */ 
(() => { 
    'use strict'; 

    // get previous setting (or set to Pinky as default) 
    let char = GM_getValue('character', 'Pinky'); 

    // do something fun! 

    // called through the userscript addon 
    GM_registerMenuCommand('Are you Pinky or Brain?',() => { 
    const value = prompt('Enter "p" or "b"', char); 
    if (value !== null) { 
     // default to Pinky 
     char = /^b/i.test(value) ? 'Brain' : 'Pinky'; 
     GM_setValue('character', char); 
    } 
    }); 

})(); 
相關問題