Greasy Fork

ACGN-Stock股票事件監聽

監聽ACGN網頁變化並給予Addevent

目前为 2017-10-09 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.greasyfork.icu/scripts/33945/222891/ACGN-Stock%E8%82%A1%E7%A5%A8%E4%BA%8B%E4%BB%B6%E7%9B%A3%E8%81%BD.js

// ==UserScript==
// @name         ACGN-Stock股票事件監聽
// @namespace    http://tampermonkey.net/
// @version      0.002
// @description  監聽ACGN網頁變化並給予Addevent
// @author       Ming
// @match        http://acgn-stock.com/*
// @match        https://acgn-stock.com/*
// @grant        none
// ==/UserScript==
// ==/UserScript==

class BaseEvent {
    constructor(pattern) {
        this.pattern = pattern;
        this.callbacklist = [];
    }
    AddEventListner(callback) { this.callbacklist.push(callback); }
    RunCallback() {
        for (let i = 0; i < callbacklist.length; i++) {
            callbacklist[i]();
        }
    }
    CheckUrllState() {
        if (document.location.href.search(this.pattern) !== -1) {
            setTimeout(RunCallback, 1000);
        }
    }
}
class Company extends BaseEvent {
    constructor() {
        super(/company\/detail/);
    }
}
class StockSummary extends BaseEvent {
    constructor() {
        super(/company\/[0-9]+/);
    }
}
class AccountInfo extends BaseEvent {
    constructor() {
        super(/accountInfo/);
    }
}
class Foundation extends BaseEvent {
    constructor() {
        super(/foundation\/[0-9]+/);
    }
}
class ACGNClass {
    constructor() {
        this.oldUrl = "";
        this.company = new Company();
        this.stockSummary = new StockSummary();
        this.accountInfo = new AccountInfo();
        this.foundation = new Foundation();
        setTimeout(this.BindMain.bind(this),5000);
    }
    BindMain(){
        $("#main").bind("DOMNodeInserted DOMNodeRemoved", this.MainDivCheck);
        console.log("ACGN-Stock Listener Done");
    }
    MainDivCheck() {
        // 因AJAX動態生成不斷執行,所以有時候main的變動並不代表換頁,此時無須重新加入事件
        if (this.oldUrl === document.location.href) return;
        oldUrl = document.location.href;

        //設定狀態
        this.company.CheckUrllState();
        this.stockSummary.CheckUrllState();
        this.accountInfo.CheckUrllState();
        this.foundation.CheckUrllState();
    }
    AddCompanyListener(callback){
        this.company.AddEventListner(callback);
    }
    AddStockSummaryListener(callback){
        this.company.AddEventListner(callback);
    }
    AddAccountInfoListener(callback){
        this.company.AddEventListner(callback);
    }
    AddFoundationListener(callback){
        this.company.AddEventListner(callback);
    }
}
var ACGNListener;
(function(){
    ACGNListener = new ACGNClass();
})();