需要Javascript帮助,存储局部变量

时间:2016-06-02 18:19:40

标签: javascript jquery google-chrome-extension

作为Javascript新手,我无法在本地存储变量并从Google缓存中重用。 我有一个从零开始onClick然后递增的计数器,直到一个特定的数字然后转到一个新的页面。

在新页面中,我需要从之前的号码继续计数器。

我无法使用Cookie,因为这是针对Chrome扩展程序的,因此我使用storage.local并将清单设置如下:

...
"permissions": ["storage", "https://example.com/*", "unlimitedStorage"],
  "content_scripts": [
    {
      "matches":  ["https://example.com/*"],
      "js":       ["js/jquery-2.1.4.min.js", "js/example.js"],
      "css":      ["css/example.css"]
    }
  ],
...

所以基本上当我点击我的按钮时,它会启动follow功能,然后递增this.count

function Button(t, e, i) {
    var n = this;
    n.$el = t, 
    n.count = (Record.get(function() {tmf.followBtn.counter()}) > 1 ? Record.get(function() {tmf.followBtn.counter()}) : 0), 
    n.$title = t.find(".tmf-btn__title"), 
    n.$subtitle = t.find(".tmf-btn__subtitle"), 
    n.action = new BulkAction(e, i), n.$el.on("click", function() {
        n.$el.addClass("tmf-btn--active"), 
        n.$title.text(n.count), 
        n.action.paused ? (n.action.proceed(), n.$subtitle.text("Click to pause")) : (n.action.pause(), n.$subtitle.text("Click to continue"))
    })
}

function Profile(t) {
    this.$el = $(".ProfileCard:eq(" + t + ")"), 
    this.isLoaded() && (this.$btn = this.$el.find(".user-actions-follow-button"), 
    this.id = this.$el.data("user-id") + "-")
}

function BulkAction(t, e) {
    this.callback = t, 
    this.interval = e, 
    this.paused = !0
}

function gotoPage() {
    window.location.replace("https://example.com/newpage/");
}

$.fn.isPresent = function() {
    return this.length > 0
};

var USER_ID = $("#user-dropdown").find("[data-user-id]").data("user-id"),
    Record = {
        storage: chrome.storage.local,
        key: USER_ID.toString(),
        get: function(t) {
            var e = this;
            this.storage.get(this.key, function(i) {
                e.result = i, t.call()
            })
        },
        set: function() {
            this.storage.set(this.result)
        },
        add: function(t) {
            this.result[this.key] ? this.result[this.key] += t : this.result[this.key] = t, this.set()
        },
        includes: function(t) {
            return this.result[this.key] ? this.result[this.key].includes(t) : !1
        }
    },
    tmf = {
        init: function() {
            var t = this;
            t.$el = $("<div>").addClass("tmf animated").appendTo("body")
        },
        toggle: function() {
            var t = this;
            this.$el.removeClass("flipInY flipOutY tmf--active"), $(".ProfileCard").length > 10 ? t.$el.load(chrome.extension.getURL("html/actions.html"), {}, function() {
                $(".tmf-btn").on("click", function() {
                    t.$el.addClass("tmf--active")
                }), t.followBtn = new Button($(".tmf-btn--follow"), function() {
                    this.follow()
                }, 100), t.unfollowBtn = new Button($(".tmf-btn--unfollow"), function() {
                    this.unfollow()
                }, 100), t.$el.addClass("flipInY");
                var e = new MutationObserver(function(e) {
                    e.forEach(function(e) {
                        var i = $("#message-drawer").html();
                        i.includes("66885") && t.followBtn.setIdle()
                    })
                });
                e.observe(document.getElementById("message-drawer"), {
                    subtree: !0,
                    characterData: !0,
                    childList: !0
                }), $("#tmf_without_exception").on("change", function() {
                    t.withoutException = this.checked, 
                    this.checked ? (t.followBtn.$subtitle.text("without exception"),t.unfollowBtn.$subtitle.text("without exception")) : (t.followBtn.$subtitle.text("who have never been unfollowed"), t.unfollowBtn.$subtitle.text("who do not follow you"))
                })
            }) : this.$el.addClass("flipOutY")
        },
        withoutException: !1
    };

$.extend(Button.prototype, {
    incrementCount: function() {
        this.count++, 
        this.$title.text(this.count)
    },
    counter: function() {
        return this.count
    },
    setIdle: function() {
        var t = this;
        if (!t.action.paused) {
            t.action.pause();
        }
    }
    }), 
    $.extend(Profile.prototype, {
        isLoaded: function() {
            return this.$el.isPresent()
        }, 
        isFollowing: function() {
            return this.$el.find(".FollowStatus").isPresent()
        },
        isProtected: function() {
            return this.$el.find(".Icon--protected").isPresent()
        },
        isNotFollowing: function() {
            return !this.isFollowing()
        },
        isFollowable: function() {
            return this.$btn.children(".follow-text").is(":visible")
        },
        isFollowed: function() {
            return this.$btn.children(".following-text").is(":visible")
        },
        follow: function() {       
            if (tmf.followBtn.counter() > 1) {
                tmf.followBtn.action.pause();
                Record.set(function() {tmf.followBtn.counter()});
                gotoPage(); 
            };        
            this.isFollowable() && !this.isProtected() && tmf.followBtn.counter() < 6 && (tmf.withoutException || !Record.includes(this.id) ? (this.click(), tmf.followBtn.incrementCount()) : this.log("warn", "Already unfollowed once"))
        },
        unfollow: function() {
            this.isFollowed() && (tmf.withoutException || this.isNotFollowing()) && (this.click(), tmf.unfollowBtn.incrementCount(), Record.add(this.id))
        },
        click: function() {
            this.log("success", 'Clicked on "' + this.$btn.children(":visible").text().trim() + '"'), this.$btn.click(), this.clicked = !0
        },
        log: function(t, e) {
            $("<div>").addClass("tmf-log").addClass("tmf-log--" + t).text(e).insertBefore(this.$el.find(".ProfileCard-bio"))
        }
    }), 
    $.extend(BulkAction.prototype, {
        nth: 0,
        loadAttempts: 0,
        _loadProfiles: function() {
            this.loadAttempts < 10 && (this.last.$btn.focus(), this.loadAttempts++, this._sleep(700))
        },
        _sleep: function(t) {
            var e = this;
            setTimeout(function() {
                e.run()
            }, t)
        },
        run: function() {
            if (!this.paused) {
                var t = new Profile(this.nth);
                t.isLoaded() ? (this.loadAttempts = 0, this.callback.call(t), this.last = t, this.nth++, t.clicked ? this._sleep(this.interval) : this.run()) : this._loadProfiles()
            }
        },
        pause: function() {
            this.paused = !0
        },
        proceed: function() {
            this.paused = !1, this.run()
        }
    }), 
    Record.get(function() {
    tmf.init(), tmf.toggle()
});

var observer = new MutationObserver(function(t) {
    t.forEach(function(t) {
        tmf.toggle()
    })
});

$(".tmf-btn--close").click(function(){
    $(".flipInY").hide();
});

observer.observe(document.querySelector("head > title"), {
    subtree: !0,
    characterData: !0,
    childList: !0
});

我需要在本地存储我的计数器,因此您可以看到我尝试set将其作为:

我的Record.set(function() {tmf.followBtn.counter()});函数中的

follow

然后我尝试get从我的Button函数中将其n.count = (Record.get(function() {tmf.followBtn.counter()}) > 1 ? Record.get(function() {tmf.followBtn.counter()}) : 0),作为:

Rsymphony

为什么我一直重新加载页面,计数器从零开始?

0 个答案:

没有答案