Userscript code works in Firefox but not in Chrome? (unsafeWindow)

家住魔仙堡 提交于 2019-12-31 05:37:11

问题


I am using this userscript in Firefox that counts unread tweets in Tweetdeck and colors the new tweets.

It works fine with Firefox (Greasemonkey), but in Chrome I'm not getting anything. Here is the code:

// ==UserScript==
// @name     TweetDeck Unread Notifications
// @include  https://tweetdeck.twitter.com
// @include  https://tweetdeck.twitter.com/*
// @grant    none
// ==/UserScript==
var head, style;
head = document.getElementsByTagName('head')[0];
style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = ".tdUnreadUnread { background-color: #444444; }";
head.appendChild(style);

function animate_bg(ele, from, to) {
ele.css("background-color", "rgba(68, 68, 68, " + (from += from > to ? -1 :    1) / 10 + ")");
if (from != to) setTimeout(function () {
    animate_bg(ele, from, to)
}, 20);
}

var counter = 0;
var loadingTime = true;

unsafeWindow.tdUnreadRefresh = function () {
var detail = $(".js-detail-content");
$("article").each(function (i) {
    if (detail.length == 0) {
        if (!$(this).hasClass("tdUnreadUnread")) {
            $(this).addClass("tdUnreadUnread");
            if (!loadingTime) {
                counter++;
                $(this).mouseenter(function () {
                    counter--;
                    $(this).off("mouseenter");
                    animate_bg($(this), 10, 0);
                });
            } else animate_bg($(this), 10, 0);
        }
    }
});

if (counter > 0) {
    document.title = "(" + counter + ") TweetDeck";
} else {
    document.title = "TweetDeck"
       }
}
  unsafeWindow.tdUnreadLoadingOver = function () {
  loadingTime = false;
  }

  setInterval("tdUnreadRefresh()", 1000);
  setTimeout("tdUnreadLoadingOver()", 30000);

Any help with this is much appreciated. Thanks Bob


回答1:


IIRC the object unsafewindow is specific to Mozilla Firefox and isn't supported with Google Chrome.
Instead of using unsafewindow, you might want to export your functions using the exportFunction feature: https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Language_Bindings/Components.utils.exportFunction



来源:https://stackoverflow.com/questions/37245124/userscript-code-works-in-firefox-but-not-in-chrome-unsafewindow

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