views:

421

answers:

2

I'm using Greasemonkey and JQuerys #css method to add css styles to a page. Script so far:

// ==UserScript==
// @name           www.al-anon.dk Remove inline scroll so that page content prints properly
// @namespace      http://userscripts.org/users/103819
// @description    remove scroll from al-anon pages
// @include        http://al-anon.dk/*
// @require        http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==

$('#framen').css({'height': 'auto', width: 'auto'});
$('#menu').css({ 'display': 'none'});

Now my question is how do I apply the last rule only for @media print?

In other words: If this were clean CSS I would use this syntax:

@media print {
  /* style sheet for print goes here */
}

But how to optain this with Greasemonkey/JQuery

+1  A: 

You could append a style element instead:

$('<style media="print">#menu {display: none;}</style>').appendTo('head');
Phil Ross
yeah, I know. But is there any way to use the other JQuery way with the #css method???
Jesper Rønn-Jensen
In this particular case (where I only want to append one simple rule), I am likely to accept your answer. I'm leaving the question open a little while if a better solution shows up :)
Jesper Rønn-Jensen
+1  A: 
GM_addStyle('@media print { #menu { display:none; } }');

Also if you want your script to run in other browsers.

/**
 * Define GM_addStyle function if one doesn't exist
 */
if( typeof GM_addStyle != 'function' )
function GM_addStyle(css)
{
    var style = document.createElement('style');
    style.innerHTML = css;
    style.type='text/css';
    document.getElementsByTagName('head')[0].appendChild(style);
}
troynt