Greasemonkey how to apply CSS rule only for @media print?

I use the Greasemonkey method and JQuerys #css to add CSS styles to the page. Script:

// ==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 to apply the last rule only for @media print?

In other words: if it were pure CSS, I would use this syntax:

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

But how to do it with Greasemonkey / jQuery

+3
source share
2 answers

Instead, you can add a style element:

$('<style media="print">#menu {display: none;}</style>').appendTo('head');
+3
source
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);
}
+3
source

Source: https://habr.com/ru/post/1722087/


All Articles