简体   繁体   English

从用户脚本添加与样式无关的jQuery UI对话框到DOM

[英]Add a style-independent jQuery UI dialog to the DOM from a userscript

I'd like to create a movable, resizable dialog independent from the style of the visited page. 我想创建一个独立于访问页面样式的可移动,可调整大小的对话框。

I've added the jQuery UI CSS: jQuery-UI is not working in my userscript without CSS, or with customization? 我添加了jQuery UI CSS: jQuery-UI在没有CSS或自定义的用户脚本中不起作用?

I've tried to create a dialog from a div, but the style gets modified by the page CSS. 我试图从div创建一个对话框,但样式会被页面CSS修改。
The contents can be unreadable if the page has unexpected / weird CSS. 如果页面具有意外/奇怪的CSS,则内容可能是不可读的。

You need to define your custom CSS for dialog DIV you want to show. 您需要为要显示的对话框DIV定义自定义CSS。 If page has really weird CSS you should override these rules with your own. 如果页面有非常奇怪的CSS,你应该用自己的规则覆盖这些规则。 You can go extreme and declare all of your custom CSS rules with '!mportant'. 你可以走极端,用'!mportant'声明你所有的自定义CSS规则。 Than you will have something like local CSS reset on that level. 在这个级别上,你将拥有像本地CSS重置一样的东西。

Below is minimal example. 以下是最小的例子。 Look in header for few sites on which I tested this script. 查看我测试此脚本的几个站点的标题。 This works for me with Greasemonkey 3.11 and Firefox 54.0.1 这适用于Greasemonkey 3.11和Firefox 54.0.1

// ==UserScript==
// @name        so_test2
// @include     https://www.google.dk/*
// @include     https://www.daily.co.jp/*
// @include     http://www.ahram.org.eg/*
// @include     https://www.almesryoon.com/*
// @require     http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
// @require     http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js
// @version     1
// @grant       none
// ==/UserScript==

// dialog DIV
$("body").append ('<div id="mydialog" style="display: none">'
+'<p>This is a jQuery Dialog.</p></div>');

$("head").append(
    '<link '
  + 'href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/le-frog/jquery-ui.min.css" '
  + 'rel="stylesheet" type="text/css">'
);
// custom CSS for dialog
$("head").append(
'<style type="text/css">'
+'#mydialog { '
+'margin: 0; padding: 0; border: 0;'
+'height:100%; max-height:100%;'
+'color:#f00 !important;'
+'font: normal 3em "Open Sans", sans-serif;'
+'font-size: 32px;'
+'vertical-align: baseline;'
+'line-height: 1; }'
+'#mydialog table { '
+'border-collapse: collapse;'
+'border-spacing: 0; }'
+'.ui-dialog-titlebar { font: italic 2em "Open Sans", sans-serif; }'
+'</style>'
);

// options for dialog, for help look in jQuery docs
var opt = {
    width:      500,
    minWidth:   400,
    minHeight:  200,
    modal: false,
    autoOpen: false,
    title:      "Draggable, sizeable dialog",
    zIndex:     1
};

$("#mydialog").dialog(opt).dialog("open");

// alternative way to position a dialog
$("#mydialog").parent().css({
    position:   "fixed",
    top:        "4.2em",
    left:       "4em",
    width:      "75ex"
});

This answer builds heavily on jQuery-UI is not working in my userscript without CSS, or with customization? 这个答案很大程度上依赖于jQuery-UI在没有CSS或自定义的用户脚本中不起作用?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM