Add dynamic div-layers (overlay) with a Greasemonkey script?

 ̄綄美尐妖づ 提交于 2020-01-03 05:06:14

问题


I am new to this so please be kind (;
I'm trying to use Greasemonkey to add div-layers and I just can't get it to work properly.

My final goal is to let the user draw a window on the screen (or make two clicks) and everything but this area should kind of fade out (added dark div-layers).
But, for now I just wanted to add a visible layer to every site. Here is my first try (which did not work at all) :

var CSSNJE = '#Test_divnje {height:100px; width:100px; background-color:red;  
             position:absolute; top: 200px; left: 400px; z-index:2147483647}';
var CSSNJE = '<style type="text/css">'+ CSSNJE +'</style>';
  document.write(CSSNJE);

var DIVNJE = '<DIV id="Test_divnje"></DIV>';
  document.write(DIVNJE);


I Googled this piece of code, which works for some pages but not many:

makeLayer('LYR1',400,250,100,100,'red',1,2147483647);

function makeLayer(id,L,T,W,H,bgColor,visible,zIndex) {
  if (document.getElementById) {
    if (document.getElementById(id)) {
      alert ('Layer with this ID already exists!');
      return;
    }
    var ST = 'position:absolute'
    +'; left:'+L
    +'; top:'+T
    +'; width:'+W
    +'; height:'+H
    +'; clip:rect(0,'+W+','+H+',0)'
    +'; visibility:'
    +(null==visible || 1==visible ? 'visible':'hidden')
    +(null==zIndex  ? '' : '; z-index:'+zIndex)
    +(null==bgColor ? '' : '; background-color:'+bgColor);

    var LR = '<DIV id='+id+' style="'+ST+'"></DIV>'

    if (document.body) {
     if (document.body.insertAdjacentHTML)
         document.body.insertAdjacentHTML("BeforeEnd",LR);
     else if (document.createElement
          &&  document.body.appendChild) {
      var newNode = document.createElement('div');
      newNode.setAttribute('id',id);
      newNode.setAttribute('style',ST);
      document.body.appendChild(newNode);
     }
    }
   }
  }


First I thought the z-index of my div-layer was too low and my div-layer was just behind the visible layers (that's why my index is so high right now) but using a higher index did not help.

I also tried do use position:fixed because I read that this would maybe help, but it didn't.

How can I make an overlay that works?


回答1:


Since you are starting out, use jQuery and (in this case) jQuery-UI. It makes coding vastly simpler, and will save you a ton of grief from trying to "reinvent the wheel" the hard way.

In jQuery-UI, two-thirds of what the question asks for is accomplished in 1 line of code:

$("#dialog").dialog ( {modal: true} );

Note that this is drag-able and sizable, right out of the box!

With Greasemonkey on Firefox, there is almost no cost to using jQuery, and jQuery-UI, either. The scripts are downloaded one time (when the script is installed or edited) and then run from the local machine. It's nice and fast.

Here is a complete Greasemonkey script that adds "a layer" with a re-sizable window to Stack Overflow pages:

// ==UserScript==
// @name        _Add a User-interactive layer to a web page.
// @namespace   _pc
// @include     http://stackoverflow.com/*
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require     http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js
// @resource    jqUI_CSS  http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css
// @resource    IconSet1  http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/images/ui-icons_222222_256x240.png
// @resource    IconSet2  http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/images/ui-icons_454545_256x240.png
// @grant       GM_addStyle
// @grant       GM_getResourceText
// @grant       GM_getResourceURL
// ==/UserScript==

//--- Add our custom dialog using jQuery. Note the multi-line string syntax.
$("body").append (
    '<div id="gmOverlayDialog">                                                     \
     Resize with the control in the lower-left, or by dragging any edge.<br><br>    \
                                                                                    \
     Click the x, or press the Escape key to close.                                 \
     </div>                                                                         \
    '
);

//--- Activate the dialog.
$("#gmOverlayDialog").dialog ( {
    modal:      true,
    title:      "Click and drag here.",
    zIndex:     83666   //-- This number doesn't need to get any higher.
} );


/**********************************************************************************
    EVERYTHING BELOW HERE IS JUST WINDOW DRESSING (pun intended).
**********************************************************************************/

/*--- Process the jQuery-UI, base CSS, to work with Greasemonkey (we are not on a server)
    and then load the CSS.

    *** Kill the useless BG images:
        url(images/ui-bg_flat_0_aaaaaa_40x100.png)
        url(images/ui-bg_flat_75_ffffff_40x100.png)
        url(images/ui-bg_glass_55_fbf9ee_1x400.png)
        url(images/ui-bg_glass_65_ffffff_1x400.png)
        url(images/ui-bg_glass_75_dadada_1x400.png)
        url(images/ui-bg_glass_75_e6e6e6_1x400.png)
        url(images/ui-bg_glass_95_fef1ec_1x400.png)
        url(images/ui-bg_highlight-soft_75_cccccc_1x100.png)

    *** Rewrite the icon images, that we use, to our local resources:
        url(images/ui-icons_222222_256x240.png)
        becomes
        url("' + GM_getResourceURL ("IconSet1") + '")
        etc.
*/
var iconSet1    = GM_getResourceURL ("IconSet1");
var iconSet2    = GM_getResourceURL ("IconSet2");
var jqUI_CssSrc = GM_getResourceText ("jqUI_CSS");
jqUI_CssSrc     = jqUI_CssSrc.replace (/url\(images\/ui\-bg_.*00\.png\)/g, "");
jqUI_CssSrc     = jqUI_CssSrc.replace (/images\/ui-icons_222222_256x240\.png/g, iconSet1);
jqUI_CssSrc     = jqUI_CssSrc.replace (/images\/ui-icons_454545_256x240\.png/g, iconSet2);

GM_addStyle (jqUI_CssSrc);

//--- Add some custom style tweaks.
GM_addStyle ( '                 \
    div.ui-widget-overlay {     \
        background: red;        \
        opacity:    0.6;        \
    }                           \
' );


来源:https://stackoverflow.com/questions/10638947/add-dynamic-div-layers-overlay-with-a-greasemonkey-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!