在油猴脚本中添加css样式的方法

由于项目要求,需要在系统页面注入dom元素,且对这些注入的元素在UI界面层有美观度要求,就避免不了要对其CSS样式优化。

通常在油猴脚本中添加CSS样式的方法如下:

一、引入外部css文件

// @resource customCSS https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css
// @grant        GM_addStyle
// @grant        GM_getResourceText
.......

   // 代码内部    引入bootstrap的css文件并加入html中
   const css = GM_getResourceText("customCSS");
   GM_addStyle(css);

二、使用油猴自带样式添加请求

// @grant        GM_addStyle
.......

// 代码内部 
var dropBox = document.createElement('div');
dropBox.setAttribute('id', 'dropbox');
GM_addStyle('#dropbox{display:none; width:400px;height:200px;padding:8px; ......}')

三、自定义样式函数

var fixButton = document.createElement('div');
setStyle(fixButton,{
    width: '100px', 
    height: '32px',
    borderRadius: '2px', 
    background: '#e33e33', 
    color:'#fff', 
    fontSize:'14px', 
    textAlign: 'center', 
    lineHeight:'32px', 
});
// 样式函数
function setStyle(dom,options,fn){
    new Promise(function(resolve,reject){
        for (let key in options){
            dom.style[key] = options[key];
        }
        resolve();
    }).then(res => {
        if (fn) {
            fn()
        }
    }).catch(err => {
        console.log(err)
    })
}

四、js添加样式

var dropBox = document.createElement('div');
dropBox.style.display = 'none';
dropBox.style.width = '400px';
dropBox.style.height = '200px';
dropBox.style.fontSize = '14px';
dropBox.style.color = '#888';
......

$('.dropbox').css({"display":"none","padding":"10px","width":"400px","height":"200px","font-size":"16px","font-weight":"bold","color":"#fff"})

以上是在油猴脚本中添加UI样式的方法

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我的2009

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值