Use greasemonkey to add HTML before table

前端 未结 1 949
长发绾君心
长发绾君心 2021-01-27 03:01

I am using greasemonkey to edit a page. I need to add my own table between the two tables that are already on the page and then remove the second table. There is nothing really

1条回答
  •  面向向阳花
    2021-01-27 03:23

    This is a good chance to introduce jQuery. jQuery will be dead useful for the other things your GM script will do, plus, it's robust and cross-browser capable (for reusing your code).

    (1) Add this line to the Greasemonkey metadata section, after the // @include directive(s):

    // @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
    

    (Note you may have to uninstall and then reinstall the script to get jQuery copied over.)

    (2) Then you can use this code to add your table and delete the old one:

    //--- Get the 2nd table with class "details".
    var jSecondTable    = $("table.details:eq(1)");
    
    //--- Insert my table before it.
    jSecondTable.before 
    (
        ''
      + '    '
      + '        '
      + '        '
      + '    '
      + '    '
      + '        '
      + '        '
      + '    '
      + '
    ' ); //--- Delete the undesired table. jSecondTable.remove (); /*--- Alternately, just hide the undesired table. jSecondTable.hide (); */


    You can see a version of this code, in action, at jsFiddle.


    Alternate method of adding your table -- Less straightforward but does not require all the quotes:

    jSecondTable.before ( (<>
            
                
                
            
            
                
                
            
        
        ]]>).toString ()
    );
    

    0 讨论(0)
提交回复
热议问题