chrome 插件开发问题记录:Cannot call method setBadgeText of undefined

  1. 想要设置插件logo的bradege,报错:(TypeError): Cannot call method 'setBadgeText' of undefined
//background.js
chrome.browserAction.setBadgeText({text: 'new'});
chrome.browserAction.setBadgeBackgroundColor({color: [255, 0, 0, 255]});

报错,(TypeError): Cannot call method 'setBadgeText' of undefined
原因:
我的manifest.json 设置的是:page_action

"page_action": {
        "default_popup": "popup.html",
        "default_icon": {
            "16": "images/get_started16.png",
          }
    }

应该修改为browser_action:

"browser_action": 
    {
        "default_icon": "images/get_started16.png",
        "default_title": "这是一个示例Chrome插件",
        "default_popup": "popup.html"
    },

2.引入外链资源
报错:
在这里插入图片描述

需要在manifest.json文件中加入配置:

"content_security_policy": "script-src 'self' https://example.com; object-src 'self'",

参考

官方文档:https://developer.chrome.com/extensions/pageAction

🌟开发全攻略:
https://www.cnblogs.com/liuxianan/p/chrome-plugin-develop.html

http://blog.haoji.me/chrome-plugin-develop.html

Chrome 插件开发入门:https://crxdoc-zh.appspot.com/extensions/getstarted

Chrome 网站获取cookie:https://segmentfault.com/a/1190000020525962

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
QListWidget本身不支持增加角标,但可以通过自定义QListWidgetItem的方式来实现。具体实现步骤如下: 1. 自定义一个QListWidgetItem的子类,例如BadgeListWidgetItem。 2. 在BadgeListWidgetItem中添加一个QLabel成员变量,用于存放角标。 3. 重写BadgeListWidgetItem的paint()方法,在该方法中绘制角标。 4. 在QListWidget中使用BadgeListWidgetItem代替QListWidgetItem,并在需要的时候设置角标。 下面是一个简单的实现示例: ``` class BadgeListWidgetItem : public QListWidgetItem { public: BadgeListWidgetItem(const QString &text, QListWidget *parent = nullptr) : QListWidgetItem(text, parent) { badgeLabel = new QLabel(this->listWidget()); badgeLabel->setAlignment(Qt::AlignCenter); badgeLabel->setStyleSheet("background-color: red; color: white;"); badgeLabel->setFixedSize(20, 20); badgeLabel->move(this->listWidget()->visualItemRect(this).topRight() - QPoint(badgeLabel->width() + 5, 0)); badgeLabel->hide(); } void setBadgeText(const QString &text) { badgeLabel->setText(text); badgeLabel->adjustSize(); badgeLabel->move(this->listWidget()->visualItemRect(this).topRight() - QPoint(badgeLabel->width() + 5, 0)); badgeLabel->setVisible(!text.isEmpty()); } protected: void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override { QListWidgetItem::paint(painter, option, index); if (badgeLabel->isVisible()) { QRect badgeRect = badgeLabel->geometry(); painter->setPen(Qt::NoPen); painter->setBrush(QColor(255, 0, 0)); painter->drawRoundedRect(badgeRect, badgeRect.height() / 2, badgeRect.height() / 2); painter->setPen(QColor(255, 255, 255)); painter->drawText(badgeRect, Qt::AlignCenter, badgeLabel->text()); } } private: QLabel *badgeLabel; }; ``` 使用示例: ``` QListWidget *listWidget = new QListWidget; BadgeListWidgetItem *item1 = new BadgeListWidgetItem("Item 1", listWidget); item1->setBadgeText("2"); BadgeListWidgetItem *item2 = new BadgeListWidgetItem("Item 2", listWidget); item2->setBadgeText("1"); listWidget->addItem(item1); listWidget->addItem(item2); ``` 这样就可以在QListWidget中显示角标了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值