Search code examples
javascriptgreasemonkey

How to grab data from one tab and paste it to another tab using greasemonkey


I want to grab some data from a page opened in one tab, and paste it into a textarea of another page opened in another browser tab. How can I do this with Javascript and Greasemonkey?


Solution

    1. Set both domains in the metadata block so the script will be activated on both pages
    2. Find an unique element in both websites from which you can detect which page you are currently on.
    3. If you are on the page with the table, get the data and put it with GM_setValue in the store. If needed, open the next website by using GM_openInTab.
    4. If the next website gets detected, retrieve the stored value with GM_getValue and paste it into the textarea.

    Not this hard over Greasemonkey, even though its necessary to load the textarea-page AFTER the table-page.

    Example

    // @include       http://website1.com/*
    // @include       http://website2.com/*
    // @require       http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js
    
    $(document).ready(function() {
        if( $("#divfromsite1").length )
        {
           GM_setValue("pastetext", $("#gettable").html() );
           GM_openInTab("http://website2.com/");
        }
        else
        {
           $("#pastetextarea").val( GM_getValue("pastetext","") );
        }
    });