From 808a61c681fe76f6234a835cdabc79972ac21d9a Mon Sep 17 00:00:00 2001 From: MoeHero Date: Thu, 4 May 2017 15:15:18 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E9=87=8D=E6=9E=84=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E7=AD=BE=E5=88=B0=20=E9=87=8D=E6=9E=84=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E9=A2=86=E7=93=9C=E5=AD=90=20=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 +- src/background.js | 26 ++++-- src/bilibili_live/ALPlugin_SmallTV.js | 2 +- src/bilibili_live/FuncAutoLottery.class.js | 7 +- src/bilibili_live/FuncGiftPackage.class.js | 4 +- src/bilibili_live/FuncLiveSetting.class.js | 7 +- src/bilibili_live/FuncSign.class.js | 46 ++++----- src/bilibili_live/FuncTreasure.class.js | 103 +++++++-------------- src/bilibili_live/Live.Util.js | 7 +- src/bilibili_live/ModuleConsole.class.js | 6 ++ src/bilibili_live/ModuleNotify.class.js | 3 + src/bilibili_live/ModuleStore.class.js | 2 +- src/options/updatelog.html | 9 +- 13 files changed, 112 insertions(+), 116 deletions(-) diff --git a/README.md b/README.md index 0c850ff..2ce46c0 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ - [ ] 切换默认排行榜页面 - [ ] 赠送礼物增强 - [ ] 主站功能 +- [ ] 自动更新 ### 后续版本功能 - [ ] 直播设置支持保存 @@ -31,10 +32,11 @@ - [x] 抽奖统计 ## Develop Progress -- 重构自动领瓜子代码 0% - 重构礼物包裹代码 0% - 重构直播设置代码 0% -- 优化程序结构 10% +- 优化程序结构 20% +- 重构设置页面 0% +- 自动更新 0% ## License ※ 本项目使用 [Mozilla Public License 2.0](https://github.com/MoeHero/BilibiliHelper/blob/master/LICENSE) 许可协议 diff --git a/src/background.js b/src/background.js index 46a3340..26bf50d 100644 --- a/src/background.js +++ b/src/background.js @@ -1,11 +1,22 @@ console.log('Start Loading...'); -var Treasure = { +var Sign = { showID: false, tabID: false, - getTreasure: function() { - return Treasure; + getSign: () => Sign, + setSign: function(options) { + Sign.showID = options.showID; + Sign.tabID = options.tabID; }, + delSign: function() { + Sign.showID = false; + Sign.tabID = false; + } +}; +var Treasure = { + showID: false, + tabID: false, + getTreasure: () => Treasure, setTreasure: function(options) { Treasure.showID = options.showID; Treasure.tabID = options.tabID; @@ -18,9 +29,7 @@ var Treasure = { var SmallTV = { showID: false, tabID: false, - getSmallTV: function() { - return SmallTV; - }, + getSmallTV: () => SmallTV, setSmallTV: function(options) { SmallTV.showID = options.showID; SmallTV.tabID = options.tabID; @@ -33,9 +42,7 @@ var SmallTV = { var Lighten = { showID: false, tabID: false, - getLighten: function() { - return Lighten; - }, + getLighten: () => Lighten, setLighten: function(options) { Lighten.showID = options.showID; Lighten.tabID = options.tabID; @@ -45,6 +52,7 @@ var Lighten = { Lighten.tabID = false; } }; + var Option = { live_autoSign: true, live_autoTreasure: true, diff --git a/src/bilibili_live/ALPlugin_SmallTV.js b/src/bilibili_live/ALPlugin_SmallTV.js index 4cfa964..712a7bf 100644 --- a/src/bilibili_live/ALPlugin_SmallTV.js +++ b/src/bilibili_live/ALPlugin_SmallTV.js @@ -26,7 +26,7 @@ class ALPlugin_SmallTV { $('.treasure-box-ctnr').remove(); this.stateIcon = $('').addClass('bh-icon tv-init'); this.stateText = $('').addClass('func-info v-top').text('初始化中...'); - Live.DOM.funcInfoRow.prepend(this.stateText).prepend(this.stateIcon); + Live.DOM.funcInfoRow.prepend(this.stateIcon, this.stateText); } static addEvent() { Live.sendMessage({command: 'getSmallTV'}, (result) => { diff --git a/src/bilibili_live/FuncAutoLottery.class.js b/src/bilibili_live/FuncAutoLottery.class.js index b4e3246..42f95bf 100644 --- a/src/bilibili_live/FuncAutoLottery.class.js +++ b/src/bilibili_live/FuncAutoLottery.class.js @@ -22,8 +22,8 @@ class FuncAutoLottery { Live.DOM.info.before($('
').addClass('ctrl-item').append(this.statinfoButton)).parent().before(this.statinfoPanel); } static addEvent() { - this.statinfoButton.on('click', (e) => {this.openStatinfoPanel();e.stopPropagation();}); - this.statinfoPanel.on('click', (e) => e.stopPropagation()); + this.statinfoButton.on('click', () => this.openStatinfoPanel()).stopPropagation(); + this.statinfoPanel.stopPropagation(); $(document).on('click', () => this.statinfoPanel.fadeOut(200)); } @@ -41,8 +41,7 @@ class FuncAutoLottery { for(let key in info.statinfo) { statinfosContent.append($('
  • ').text(key + 'x' + info.statinfo[key])); } - this.statinfoPanel.append($('

    ').addClass('bh-title').text(info.name)) - .append($('').addClass('f-right').text(info.times + ' 次')).append($('
    ')).append(statinfosContent); + this.statinfoPanel.append($('

    ').addClass('bh-title').text(info.name), $('').addClass('f-right').text(info.times + ' 次'), $('
    '), statinfosContent); } this.statinfoPanel.fadeIn(200); } diff --git a/src/bilibili_live/FuncGiftPackage.class.js b/src/bilibili_live/FuncGiftPackage.class.js index 855d4c5..03a1d9b 100644 --- a/src/bilibili_live/FuncGiftPackage.class.js +++ b/src/bilibili_live/FuncGiftPackage.class.js @@ -33,8 +33,8 @@ class FuncGiftPackage { $('#gift-package-send-panel').after(this.sendPanel).remove(); } static addEvent() { - this.packagePanel.on('click', (event) => event.stopPropagation()); - this.sendPanel.on('click', (event) => event.stopPropagation()); + this.packagePanel.stopPropagation(); + this.sendPanel.stopPropagation(); $(document).on('click', () => this.packagePanel.fadeOut(200)); this.packageButton.on('click', () => this.openGiftPackage()); diff --git a/src/bilibili_live/FuncLiveSetting.class.js b/src/bilibili_live/FuncLiveSetting.class.js index c130def..f498ab4 100644 --- a/src/bilibili_live/FuncLiveSetting.class.js +++ b/src/bilibili_live/FuncLiveSetting.class.js @@ -33,13 +33,10 @@ class FuncLiveSetting { $('.profile-ctrl').append(this.liveSettingPanel).append(this.liveSettingButton); } static addEvent() { - this.liveSettingPanel.on('click', (event) => event.stopPropagation()); + this.liveSettingPanel.stopPropagation(); $(document).on('click', () => this.liveSettingPanel.fadeOut(200)); - this.liveSettingButton.on('click', (event) => { - this.liveSettingPanel.fadeToggle(200); - event.stopPropagation(); - }); + this.liveSettingButton.on('click', () => this.liveSettingPanel.fadeToggle(200)).stopPropagation(); $('.bh-live-setting-btns').on('click', (event) => { let button = $(event.currentTarget); this.funcList[button.attr('key')].click.call(this); diff --git a/src/bilibili_live/FuncSign.class.js b/src/bilibili_live/FuncSign.class.js index 8cbd61e..9d92aa1 100644 --- a/src/bilibili_live/FuncSign.class.js +++ b/src/bilibili_live/FuncSign.class.js @@ -1,32 +1,26 @@ -/* globals ModuleStore,ModuleDom,ModuleNotify,ModuleConsole */ +/* globals ModuleStore,ModuleNotify,ModuleConsole */ class FuncSign { static init() { if(!Live.option.live || !Live.option.live_autoSign) { return; } - this.event('enabled'); - Live.timer(60 * 60 * 1000, () => this.do()); + this.addEvent(); } - static event(key, param = {}) { - switch(key) { - case 'enabled': + static addEvent() { + Live.sendMessage({command: 'getSign'}, (result) => { + if(!result.showID) { + Live.sendMessage({command: 'setSign', showID: Live.showID}); + $(window).on('beforeunload', () => Live.sendMessage({command: 'getSign'}, (result) => result.showID == Live.showID && Live.sendMessage({command: 'delSign'}))); ModuleNotify.sign('enabled'); ModuleConsole.sign('enabled'); - break; - case 'award': - ModuleStore.sign('set'); - this.setSigned(); - ModuleNotify.sign('award', param); - ModuleConsole.sign('award', param); - break; - case 'exist': - ModuleStore.sign('set'); - ModuleConsole.sign('exist'); - break; - - } + Live.timer(60 * 60 * 1000, () => this.doSign()); + } else { + ModuleConsole.sign('exist', result); + } + }); } + static setSigned() { let signBtn = $('.sign-up-btn'); signBtn.find('.dp-inline-block>span:first-child').hide(); @@ -34,17 +28,23 @@ class FuncSign { signBtn.find('.has-new').removeClass('has-new'); } - static do() { + static doSign() { if(!ModuleStore.sign('get')) { $.getJSON('/sign/doSign').done((result) => { if(result.code === 0) { - this.event('award', {award: result.data.text}); + let award = {award: result.data.text}; + ModuleStore.sign('set'); + this.setSigned(); + ModuleNotify.sign('award', award); + ModuleConsole.sign('award', award); } else if(result.code == -500) { //已签到 - this.event('exist'); + ModuleStore.sign('set'); + ModuleNotify.sign('signed'); + ModuleConsole.sign('signed'); } else { console.log(result); } - }).fail(() => Live.countdown(2, () => this.do())); + }).fail(() => Live.countdown(2, () => this.doSign())); } } } diff --git a/src/bilibili_live/FuncTreasure.class.js b/src/bilibili_live/FuncTreasure.class.js index 5e65e3d..4eb530e 100644 --- a/src/bilibili_live/FuncTreasure.class.js +++ b/src/bilibili_live/FuncTreasure.class.js @@ -1,4 +1,4 @@ -/* globals ModuleStore,ModuleDom,ModuleNotify,ModuleConsole */ +/* globals ModuleStore,ModuleNotify,ModuleConsole */ class FuncTreasure { static init() { if(!Live.option.live || !Live.option.live_autoTreasure) { @@ -14,8 +14,8 @@ class FuncTreasure { this.stateText = $('').text('初始化中...'); this.timesDom = $('').text('0/0').hide(); this.countdownDom = $('').text('00:00').hide(); - let funcInfo = $('
    ').addClass('func-info v-top').append(this.stateText).append(this.timesDom).append(' ').append(this.countdownDom); - Live.DOM.funcInfoRow.prepend(funcInfo).prepend(this.stateIcon); + let funcInfo = $('').addClass('func-info v-top').append(this.stateText, this.timesDom, ' ', this.countdownDom); + Live.DOM.funcInfoRow.prepend(this.stateIcon, funcInfo); } static addEvent() { Live.sendMessage({command: 'getTreasure'}, (result) => { @@ -24,70 +24,29 @@ class FuncTreasure { $(window).on('beforeunload', () => { Live.sendMessage({command: 'getTreasure'}, (result) => result.showID == Live.showID && Live.sendMessage({command: 'delTreasure'})); }); - this.event('enabled'); + ModuleNotify.treasure('enabled'); + ModuleConsole.treasure('enabled'); Live.timer(60 * 60 * 1000, () => this.checkNewTask()); } else { - this.event('exist', result); + this.setStateIcon('exist'); + this.setStateText(Live.format(Live.localize.treasure.action.exist, result)); + ModuleConsole.treasure('exist', result); } }); } - static event(key, param) { - switch(key) { - case 'enabled': - ModuleNotify.treasure('enabled'); - ModuleConsole.treasure('enabled'); - break; - case 'processing': - this.stateIcon.attr('class', 'bh-icon treasure-processing'); - break; - case 'awarding': - this.stateText.text('领取中...').show(); - this.timesDom.hide(); - this.countdownDom.hide(); - break; - case 'noLogin': - this.stateIcon.attr('class', 'bh-icon treasure-error'); - this.stateText.text(Live.localize.treasure.action.noLogin).show(); - this.timesDom.hide(); - this.countdownDom.hide(); - ModuleNotify.treasure('noLogin'); - ModuleConsole.treasure('noLogin'); - break; - case 'noPhone': - this.stateIcon.attr('class', 'bh-icon treasure-error'); - this.stateText.text(Live.localize.treasure.action.noPhone).show(); - this.timesDom.hide(); - this.countdownDom.hide(); - ModuleNotify.treasure('noPhone'); - ModuleConsole.treasure('noPhone'); - break; - case 'end': - ModuleStore.treasure('end'); - this.stateIcon.attr('class', 'bh-icon treasure-end'); - this.stateText.text(Live.localize.treasure.action.end).show(); - this.timesDom.hide(); - this.countdownDom.hide(); - ModuleNotify.treasure('end'); - ModuleConsole.treasure('end'); - break; - case 'exist': - this.stateIcon.attr('class', 'bh-icon treasure-exist'); - this.stateText.text(Live.format(Live.localize.treasure.action.exist, param)).show(); - this.timesDom.hide(); - this.countdownDom.hide(); - ModuleConsole.treasure('exist', param); - break; - case 'award': - ModuleNotify.treasure('award', param); - ModuleConsole.treasure('award', param); - break; - } - } static setTimes(times) { this.timesDom.text(times).show(); this.stateText.hide(); } + static setStateText(text) { + this.stateText.text(text).show(); + this.timesDom.hide(); + this.countdownDom.hide(); + } + static setStateIcon(key) { + this.stateIcon.attr('class', 'bh-icon treasure-' + key); + } static checkNewTask() { if(!ModuleStore.treasure('getEnd')) { @@ -98,25 +57,30 @@ class FuncTreasure { this.endTime = result.data.time_end; this.countdown && this.countdown.clearCountdown(); this.countdown = new Live.countdown(result.data.minute * 60, () => { - this.event('awarding'); + this.setStateText('领取中...'); this.getAward(); }, this.countdownDom.show()); this.stateText.hide(); - this.event('processing'); + this.setStateIcon('processing'); } else if(result.code == -101) { //未登录 - this.event('noLogin'); + this.setStateIcon('error'); + this.setStateText(Live.localize.treasure.action.noLogin); + ModuleNotify.treasure('noLogin'); + ModuleConsole.treasure('noLogin'); } else if(result.code == -10017) { //领取完毕 - this.event('end'); + ModuleStore.treasure('end'); + this.setStateIcon('end'); + this.setStateText(Live.localize.treasure.action.end); + ModuleNotify.treasure('end'); + ModuleConsole.treasure('end'); } else { console.log(result); } }).fail(() => Live.countdown(2, () => this.checkNewTask())); } else { ModuleStore.treasure('end'); - this.stateIcon.attr('class', 'bh-icon treasure-end'); - this.stateText.text(Live.localize.treasure.action.end).show(); - this.timesDom.hide(); - this.countdownDom.hide(); + this.setStateIcon('end'); + this.setStateText(Live.localize.treasure.action.end); } } static getAward() { @@ -125,7 +89,9 @@ class FuncTreasure { this.answer = eval(this.correctQuestion(OCRAD(image))); //jshint ignore:line $.getJSON('/FreeSilver/getAward', {time_start: this.startTime, time_end: this.endTime, captcha: this.answer}).done((result) => { if(result.code === 0) { - this.event('award', {award: result.data.awardSilver, silver: result.data.silver}); + let award = {award: result.data.awardSilver, silver: result.data.silver}; + ModuleNotify.treasure('award', award); + ModuleConsole.treasure('award', award); Live.addScriptByText(`bh_updateSilverSeed(${result.data.silver});`).remove(); this.checkNewTask(); } else if(result.code == -99) { //在其他地方领取 @@ -133,7 +99,10 @@ class FuncTreasure { } else if(result.code == -400 && result.msg.includes('验证码')) { //验证码出错 this.getAward(); } else if(result.code == -400 && result.msg == '未绑定手机') { //未绑定手机 - this.event('noPhone'); + this.setStateIcon('error'); + this.setStateText(Live.localize.treasure.action.noPhone); + ModuleNotify.treasure('noPhone'); + ModuleConsole.treasure('noPhone'); } else { console.log(result); } diff --git a/src/bilibili_live/Live.Util.js b/src/bilibili_live/Live.Util.js index aa4053d..564c61b 100644 --- a/src/bilibili_live/Live.Util.js +++ b/src/bilibili_live/Live.Util.js @@ -12,7 +12,8 @@ Live.localize = {//TODO 重构 去除不必要文本 title: '自动签到', action: { award: '签到成功, 获得${award}', - exist: '今日已签到' + exist: '已在直播间${showID}启动', + signed: '今日已签到' } }, treasure: { @@ -96,3 +97,7 @@ Live.timer.prototype.clearTimer = function() { Live.sendMessage = (msg, callback) => chrome.runtime.sendMessage(msg, (response) => typeof callback == 'function' && callback(response)); Live.getMessage = (callback) => chrome.runtime.onMessage.addListener((request, sender, sendResponse) => typeof callback == 'function' && callback(request, sender, sendResponse)); + +$.fn.stopPropagation = function() { + return this.on('click', (e) => e.stopPropagation()); +}; diff --git a/src/bilibili_live/ModuleConsole.class.js b/src/bilibili_live/ModuleConsole.class.js index 9febb03..1815f77 100644 --- a/src/bilibili_live/ModuleConsole.class.js +++ b/src/bilibili_live/ModuleConsole.class.js @@ -19,6 +19,12 @@ class ModuleConsole { case 'award': msg += Live.format(sign.action.award, param); break; + case 'exist': + msg += Live.format(sign.action.exist, param); + break; + case 'signed': + msg += Live.format(sign.action.signed, param); + break; } this.info(msg); } diff --git a/src/bilibili_live/ModuleNotify.class.js b/src/bilibili_live/ModuleNotify.class.js index 674730c..f669a2f 100644 --- a/src/bilibili_live/ModuleNotify.class.js +++ b/src/bilibili_live/ModuleNotify.class.js @@ -22,6 +22,9 @@ class ModuleNotify { case 'award': msg = Live.format(sign.action.award, param); break; + case 'signed': + msg += Live.format(sign.action.signed, param); + break; } Live.option.notify_autoSign && this.create('sign_' + key, 'sign.png', Live.localize.helper + ' - ' + sign.title, msg); } diff --git a/src/bilibili_live/ModuleStore.class.js b/src/bilibili_live/ModuleStore.class.js index 1685a16..26e8e22 100644 --- a/src/bilibili_live/ModuleStore.class.js +++ b/src/bilibili_live/ModuleStore.class.js @@ -8,7 +8,7 @@ class ModuleStore { !store.get('BH_SmallTVTimes') && store.set('BH_SmallTVTimes', 0); !store.get('BH_LightenTimes') && store.set('BH_LightenTimes', 0); - store.get('BH_SmallTVCount') !== undefined && store.set('BH_SmallTVTimes', store.get('BH_LightenTimes') + store.get('BH_SmallTVCount')) && store.remove('BH_SmallTVCount'); + //store.get('BH_SmallTVCount') !== undefined && store.set('BH_SmallTVTimes', store.get('BH_SmallTVTimes') + store.get('BH_SmallTVCount')) && store.remove('BH_SmallTVCount'); this.list = { 'lighten': 'BH_Lighten', diff --git a/src/options/updatelog.html b/src/options/updatelog.html index e4b1eec..898c87f 100644 --- a/src/options/updatelog.html +++ b/src/options/updatelog.html @@ -1,5 +1,13 @@

    更新日志

    V0.0.11

    +

    新增

    + +

    优化 自动签到仅在一个直播间开启

    +

    优化

    +

    优化

    +

    修复

    +
    +

    V0.0.11

    新增 直播设置支持更多选项

    优化 取消检测其他插件机制

    @@ -7,7 +15,6 @@

    更新日志

    优化 自动领取瓜子后瓜子数量动态更新

    修复 自动领瓜子一直显示领取中的问题


    -

    V0.0.10

    新增 自动领取应援棒通知支持开关

    优化 检测到哔哩哔哩助手启用时将自动禁用插件防止冲突

    From 502c4eb849a8b29ffc8c8c69a0097b4bbc6c3996 Mon Sep 17 00:00:00 2001 From: MoeHero Date: Thu, 4 May 2017 18:58:57 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E9=87=8D=E6=9E=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gulpfile.js | 2 +- src/background.js | 13 +- src/bilibili_live/ALPlugin_Lighten.js | 35 ++--- src/bilibili_live/ALPlugin_SmallTV.js | 49 +++---- ...utoLottery.class.js => FuncAutoLottery.js} | 0 ...iftPackage.class.js => FuncGiftPackage.js} | 0 ...iveSetting.class.js => FuncLiveSetting.js} | 0 .../{FuncSign.class.js => FuncSign.js} | 0 ...{FuncTreasure.class.js => FuncTreasure.js} | 0 src/bilibili_live/Live.Util.js | 103 -------------- src/bilibili_live/Live.js | 134 ++++++++++++++++-- ...oduleConsole.class.js => ModuleConsole.js} | 3 - ...{ModuleNotify.class.js => ModuleNotify.js} | 0 .../{ModuleStore.class.js => ModuleStore.js} | 0 src/bilibili_live_inject.js | 3 + 15 files changed, 170 insertions(+), 172 deletions(-) rename src/bilibili_live/{FuncAutoLottery.class.js => FuncAutoLottery.js} (100%) rename src/bilibili_live/{FuncGiftPackage.class.js => FuncGiftPackage.js} (100%) rename src/bilibili_live/{FuncLiveSetting.class.js => FuncLiveSetting.js} (100%) rename src/bilibili_live/{FuncSign.class.js => FuncSign.js} (100%) rename src/bilibili_live/{FuncTreasure.class.js => FuncTreasure.js} (100%) delete mode 100644 src/bilibili_live/Live.Util.js rename src/bilibili_live/{ModuleConsole.class.js => ModuleConsole.js} (96%) rename src/bilibili_live/{ModuleNotify.class.js => ModuleNotify.js} (100%) rename src/bilibili_live/{ModuleStore.class.js => ModuleStore.js} (100%) diff --git a/gulpfile.js b/gulpfile.js index 6ab5eca..4a73d60 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -32,7 +32,7 @@ gulp.task('set:d', function() { gulp.task('live', function() { return gulp.src('./src/bilibili_live/*.js') - .pipe($.order(['Live*.js', 'Module*.class.js', 'Func*.class.js', '!Core.js', 'Core.js'])) + .pipe($.order(['Live.js', 'Module*.js', 'Func*.js', '!Core.js', 'Core.js'])) .pipe($.jshintChannel()) .pipe($.concat('bilibili_live.js')) .pipe($.if(path == 'release', $.babel({presets: ['babili']}))) diff --git a/src/background.js b/src/background.js index 26bf50d..869009b 100644 --- a/src/background.js +++ b/src/background.js @@ -103,6 +103,16 @@ chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tabInfo) { chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { switch(request.command) { + case 'getSign': + sendResponse(Sign.getSign()); + break; + case 'setSign': + Sign.setSign({tabID: sender.tab.id, showID: request.showID}); + break; + case 'delSign': + sendResponse(Sign.delSign()); + break; + case 'getTreasure': sendResponse(Treasure.getTreasure()); break; @@ -112,9 +122,6 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) { case 'delTreasure': sendResponse(Treasure.delTreasure()); break; - case 'checkNewTask': - chrome.tabs.sendMessage(Treasure.tabID, {command: 'checkNewTask'}); - break; case 'getSmallTV': sendResponse(SmallTV.getSmallTV()); diff --git a/src/bilibili_live/ALPlugin_Lighten.js b/src/bilibili_live/ALPlugin_Lighten.js index eceafad..5f92bd3 100644 --- a/src/bilibili_live/ALPlugin_Lighten.js +++ b/src/bilibili_live/ALPlugin_Lighten.js @@ -8,9 +8,10 @@ class ALPlugin_Lighten { this.addEvent(); } static getInfo() { - let info = {}; - info.name = '应援棒'; - info.times = ModuleStore.getTimes('lighten'); + let info = { + name: '应援棒', + times: ModuleStore.getTimes('lighten') + }; info.statinfo = {'应援棒': info.times}; return info; } @@ -25,30 +26,14 @@ class ALPlugin_Lighten { this.getLightenID(request.url.match(/com\/(.+)/)[1]); } }); - this.event('enabled'); + ModuleNotify.lighten('enabled'); + ModuleConsole.lighten('enabled'); } else { - this.event('exist', result); + ModuleConsole.lighten('exist', result); } }); } - static event(key, param) { - switch(key) { - case 'enabled': - ModuleNotify.lighten('enabled'); - ModuleConsole.lighten('enabled'); - break; - case 'exist': - ModuleConsole.lighten('exist', param); - break; - case 'award': - ModuleStore.addTimes('lighten', 1); - ModuleNotify.lighten('award'); - ModuleConsole.lighten('award'); - break; - } - } - static getLightenID(showID) { Live.getRoomID(showID, (roomID) => { $.getJSON('//api.live.bilibili.com/activity/v1/NeedYou/getLiveInfo', {roomid: roomID}).done((result) => { @@ -66,8 +51,10 @@ class ALPlugin_Lighten { static getAward(roomID, lightenID) { $.post('//api.live.bilibili.com/activity/v1/NeedYou/getLiveAward', {roomid: roomID, lightenId: lightenID}).done((result) => { if(result.code === 0) { - this.event('award'); - } else if(result.code == -400) { + ModuleStore.addTimes('lighten', 1); + ModuleNotify.lighten('award'); + ModuleConsole.lighten('award'); + } else if(result.code == -400) { //错误 } else { console.log(result); } diff --git a/src/bilibili_live/ALPlugin_SmallTV.js b/src/bilibili_live/ALPlugin_SmallTV.js index 712a7bf..4ce2d04 100644 --- a/src/bilibili_live/ALPlugin_SmallTV.js +++ b/src/bilibili_live/ALPlugin_SmallTV.js @@ -1,4 +1,4 @@ -/* globals ModuleStore,ModuleDom,ModuleNotify,ModuleConsole */ +/* globals ModuleStore,ModuleNotify,ModuleConsole */ class ALPlugin_SmallTV { static init() { this.countdown = {}; @@ -11,10 +11,11 @@ class ALPlugin_SmallTV { this.addEvent(); } static getInfo() { - let info = {}; - info.name = '小电视抽奖'; - info.times = ModuleStore.getTimes('smallTV'); - info.statinfo = []; + let info = { + name: '小电视抽奖', + times: ModuleStore.getTimes('smallTV'), + statinfo: [] + }; let statinfos = ModuleStore.getStatinfo('smallTV'); for(let key in statinfos) { info.statinfo[this.awardName[key]] = statinfos[key]; @@ -38,32 +39,23 @@ class ALPlugin_SmallTV { this.join(request.real_roomid, request.tv_id); } }); - this.event('enabled'); + this.setStateIcon('enabled'); + this.setStateText(Live.localize.enabled); + ModuleNotify.smallTV('enabled'); + ModuleConsole.smallTV('enabled'); } else { - this.event('exist', result); + this.setStateIcon('exist'); + this.setStateText(Live.format(Live.localize.smallTV.action.exist, result)); + ModuleConsole.smallTV('exist', result); } }); } - static event(key, param) { - switch(key) { - case 'enabled': - this.stateIcon.attr('class', 'bh-icon tv-enabled'); - this.stateText.text(Live.localize.enabled); - ModuleNotify.smallTV('enabled'); - ModuleConsole.smallTV('enabled'); - break; - case 'exist': - this.stateIcon.attr('class', 'bh-icon tv-exist'); - this.stateText.text(Live.format(Live.localize.smallTV.action.exist, param)); - ModuleConsole.smallTV('exist', param); - break; - case 'award': - ModuleStore.addTimes('smallTV', 1); - ModuleNotify.smallTV('award', param); - ModuleConsole.smallTV('award', param); - break; - } + static setStateText(text) { + this.stateText.text(text); + } + static setStateIcon(key) { + this.stateIcon.attr('class', 'bh-icon tv-' + key); } static join(roomID, TVID) { @@ -82,8 +74,11 @@ class ALPlugin_SmallTV { $.getJSON('/SmallTV/getReward', {id: TVID}).done((result) => { result = result.data; if(result.status === 0) { + let award = {awardNumber: result.reward.num, awardName: this.awardName[result.reward.id]}; ModuleStore.addStatinfo('smallTV', result.reward.id, result.reward.num); - this.event('award', {awardNumber: result.reward.num, awardName: this.awardName[result.reward.id]}); + ModuleStore.addTimes('smallTV', 1); + ModuleNotify.smallTV('award', award); + ModuleConsole.smallTV('award', award); } else if(result.status == 2) { //正在开奖 Live.countdown(10, () => this.getAward(TVID)); } else { diff --git a/src/bilibili_live/FuncAutoLottery.class.js b/src/bilibili_live/FuncAutoLottery.js similarity index 100% rename from src/bilibili_live/FuncAutoLottery.class.js rename to src/bilibili_live/FuncAutoLottery.js diff --git a/src/bilibili_live/FuncGiftPackage.class.js b/src/bilibili_live/FuncGiftPackage.js similarity index 100% rename from src/bilibili_live/FuncGiftPackage.class.js rename to src/bilibili_live/FuncGiftPackage.js diff --git a/src/bilibili_live/FuncLiveSetting.class.js b/src/bilibili_live/FuncLiveSetting.js similarity index 100% rename from src/bilibili_live/FuncLiveSetting.class.js rename to src/bilibili_live/FuncLiveSetting.js diff --git a/src/bilibili_live/FuncSign.class.js b/src/bilibili_live/FuncSign.js similarity index 100% rename from src/bilibili_live/FuncSign.class.js rename to src/bilibili_live/FuncSign.js diff --git a/src/bilibili_live/FuncTreasure.class.js b/src/bilibili_live/FuncTreasure.js similarity index 100% rename from src/bilibili_live/FuncTreasure.class.js rename to src/bilibili_live/FuncTreasure.js diff --git a/src/bilibili_live/Live.Util.js b/src/bilibili_live/Live.Util.js deleted file mode 100644 index 564c61b..0000000 --- a/src/bilibili_live/Live.Util.js +++ /dev/null @@ -1,103 +0,0 @@ -Live.format = (template, data) => { - if(data) { - let keys = Object.keys(data); - let dataList = keys.map((key) => data[key]); - return new Function(keys.join(','), 'return `' + template + '`;').apply(null, dataList); //jshint ignore:line - } -}; -Live.localize = {//TODO 重构 去除不必要文本 - helper: 'Bilibili助手', - enabled: '已启用', - sign: { - title: '自动签到', - action: { - award: '签到成功, 获得${award}', - exist: '已在直播间${showID}启动', - signed: '今日已签到' - } - }, - treasure: { - title: '自动领瓜子', - action: { - award: '已领取${award}瓜子', - exist: '已在直播间${showID}启动', - totalSilver: '总瓜子:${silver}', - noLogin: '未登录', - noPhone: '未绑定手机', - end: '领取完毕' - } - }, - smallTV: { - title: '自动小电视', - action: { - award: '获得${awardName}x${awardNumber}', - exist: '已在直播间${showID}启动', - joinSuccess: '参加成功', - joinError: '参加失败, ${msg}' - } - }, - lighten: { - title: '自动领取应援棒', - action: { - award: '获得应援棒x1', - exist: '已在直播间${showID}启动' - } - } -}; - -Live.countdown = function(time, callback, element) { - if(!(this instanceof Live.countdown)) { - return new Live.countdown(time, callback, element); - } - if(!time || (!(time instanceof Date) && isNaN(time))) { - console.error('时间设置错误!'); - return; - } - if(!isNaN(time)) { - let _time = new Date(); - _time.setMilliseconds(_time.getMilliseconds() + time * 1000); - time = _time; - } - let countdown = setInterval(() => { - let _time = Math.round((time.getTime() - new Date().getTime()) / 1000); - if(element instanceof jQuery) { - let min = Math.floor(_time / 60); - let sec = Math.floor(_time % 60); - min = min < 10 ? '0' + min : min; - sec = sec < 10 ? '0' + sec : sec; - element.text(min + ':' + sec); - } - if(_time <= 0) { - clearInterval(countdown); - typeof callback == 'function' && callback(); - } - }, 100); - this.countdown = countdown; -}; -Live.countdown.prototype.clearCountdown = function() { - clearInterval(this.countdown); -}; - -Live.timer = function(ms, callback) { - if(!ms || isNaN(ms)) { - console.error('时间设置错误!'); - return; - } - if(!(this instanceof Live.timer)) { - return new Live.timer(ms, callback); - } - this.timer = setInterval(() => { - typeof callback == 'function' && callback(); - }, ms); - typeof callback == 'function' && callback(); -}; -Live.timer.prototype.clearTimer = function() { - clearInterval(this.timer); -}; - -Live.sendMessage = (msg, callback) => chrome.runtime.sendMessage(msg, (response) => typeof callback == 'function' && callback(response)); -Live.getMessage = (callback) => chrome.runtime.onMessage.addListener((request, sender, sendResponse) => typeof callback == 'function' && callback(request, sender, sendResponse)); - -$.fn.stopPropagation = function() { - return this.on('click', (e) => e.stopPropagation()); -}; diff --git a/src/bilibili_live/Live.js b/src/bilibili_live/Live.js index af19c65..1ee5eab 100644 --- a/src/bilibili_live/Live.js +++ b/src/bilibili_live/Live.js @@ -1,8 +1,11 @@ -/* globals ModuleStore,ModuleNotify */ -var Live = {options: {}, userInfo: {}, DOM: {}}; -Live.showID = (function() { - return location.pathname.substr(1); -}()); +/* globals ModuleStore */ +var Live = { + options: {}, + userInfo: {}, + DOM: {}, + showID: location.pathname.substr(1) +}; + Live.addScriptByFile = function(fileName) { let script = $('