From d4f7a9d29d64e69ad6b91e807b895cab0b746215 Mon Sep 17 00:00:00 2001 From: ymrl Date: Fri, 27 Sep 2013 02:02:27 +0900 Subject: [PATCH 1/2] =?UTF-8?q?background/options=E3=81=A7=E5=85=B1?= =?UTF-8?q?=E9=80=9A=E3=81=AE=E5=86=85=E5=AE=B9=E3=82=92lib,=20consts?= =?UTF-8?q?=E3=81=AB=E3=81=8F=E3=81=8F=E3=82=8A=E5=87=BA=E3=81=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/background.coffee | 27 ++------------------------- src/consts.coffee | 2 ++ src/lib.coffee | 36 ++++++++++++++++++++++++++++++++++++ src/options.coffee | 23 +++++++++++------------ 4 files changed, 51 insertions(+), 37 deletions(-) create mode 100644 src/consts.coffee create mode 100644 src/lib.coffee diff --git a/src/background.coffee b/src/background.coffee index d57a111..41e36bd 100644 --- a/src/background.coffee +++ b/src/background.coffee @@ -1,31 +1,8 @@ -DAYS = ["mo", "tu", "we", "th", "fr", "sa", "su"] - -loadSchedule = ()-> - if localStorage.schedule - return JSON.parse(localStorage.schedule) - else - return null -loadConfig = ()-> - if localStorage.config - return JSON.parse(localStorage.config) - else - return null - -checkTime = ()-> - schedule = loadSchedule() - if !schedule - return true - else - now = new Date() - day = DAYS[now.getDay() - 1] - hour = now.getHours() - return schedule["#{day}#{hour}"] - checkTimeForResponse = (req,sender,sendResponse)-> sendResponse type: "checkTime" - playable: checkTime() - config: loadConfig() + playable: Kincolle.Schedule.isDatePlayable(new Date()) + config: Kincolle.Config.load() chrome.runtime.onInstalled.addListener ()-> if !localStorage.schedule diff --git a/src/consts.coffee b/src/consts.coffee new file mode 100644 index 0000000..807205d --- /dev/null +++ b/src/consts.coffee @@ -0,0 +1,2 @@ +window.Kincolle ||= {} +Kincolle.DAY_KEYS = ["mo", "tu", "we", "th", "fr", "sa", "su"] diff --git a/src/lib.coffee b/src/lib.coffee new file mode 100644 index 0000000..784b1d0 --- /dev/null +++ b/src/lib.coffee @@ -0,0 +1,36 @@ +window.Kincolle ||= {} + + +Kincolle.Schedule = {} +Kincolle.Schedule.load = ()-> + if localStorage.schedule + return JSON.parse(localStorage.schedule) + else + return null + +Kincolle.Schedule.save = (data)-> + localStorage.schedule = JSON.stringify(data) + +Kincolle.Schedule.isDatePlayable =(date)-> + schedule = Kincolle.Schedule.load() + if schedule + day = Kincolle.DAY_KEYS[date.getDay() - 1] + hour = date.getHours() + return schedule["#{day}#{hour}"] + else + return true + +Kincolle.Config = {} +Kincolle.Config.load = ()-> + if localStorage.config + return JSON.parse(localStorage.config) + else + return null + +Kincolle.Config.save = (config)-> + current = Kincolle.Config.load() + if current + for key of config + current[key] = config[key] + config = current + localStorage.config = JSON.stringify(config) diff --git a/src/options.coffee b/src/options.coffee index 2c0bc75..ec0369c 100644 --- a/src/options.coffee +++ b/src/options.coffee @@ -1,28 +1,27 @@ -dateKeys = [ "mo", "tu", "we", "th", "fr", "sa", "su"] - - save = (scheduleTable)-> - localStorage.schedule = JSON.stringify(scheduleTableToObject(scheduleTable)) + Kincolle.Schedule.save(scheduleTableToObject(scheduleTable)) config = useGGKSMKK:$('#useGGKSMKK').prop('checked') - localStorage.config = JSON.stringify(config) - + Kincolle.Config.save(config) load = (scheduleTable)-> - if localStorage.schedule - scheduleObjectToTable(scheduleTable,JSON.parse(localStorage.schedule)) + schedule = Kincolle.Schedule.load() + if schedule + scheduleObjectToTable(scheduleTable,schedule) else scheduleTable.find('input').each ()-> toggleCheck $(this),true,scheduleTable - if localStorage.config - setOtherConfig(JSON.parse(localStorage.config)) + setOtherConfig(Kincolle.Config.load()) setOtherConfig = (config)-> + if !config + return null if config.useGGKSMKK $('#useGGKSMKK').prop('checked',config.useGGKSMKK) + getDate = (elm)-> e = $(elm) - for k in dateKeys + for k in Kincolle.DAY_KEYS if e.hasClass(k) return k @@ -95,7 +94,7 @@ $ -> save(scheduleTable) scheduleTable.find(".date").click (e)-> t = $(@) - for k in dateKeys + for k in Kincolle.DAY_KEYS if t.hasClass k toggleDateAll scheduleTable,k,!t.hasClass('checked') break From efd7f56b7c1aba248f2c53f9e82d5a0da684e256 Mon Sep 17 00:00:00 2001 From: ymrl Date: Fri, 27 Sep 2013 22:31:04 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E8=89=A6=E3=81=93=E3=82=8C=E3=82=A6?= =?UTF-8?q?=E3=82=A3=E3=82=B8=E3=82=A7=E3=83=83=E3=83=88=E3=81=B8=E3=81=AE?= =?UTF-8?q?=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/iframe-replace.html | 16 ++++++++ assets/manifest.json | 9 ++++- assets/options.html | 2 + src/background.coffee | 25 ++++++++++-- src/content-messaging.coffee | 23 +++++++++++ src/content-views.coffee | 40 +++++++++++++++++++ src/content.coffee | 75 +++++++++--------------------------- src/iframe-content.coffee | 6 +++ src/iframe-replace.coffee | 33 ++++++++++++++++ src/lib.coffee | 32 +++++++++++++-- 10 files changed, 196 insertions(+), 65 deletions(-) create mode 100644 assets/iframe-replace.html create mode 100644 src/content-messaging.coffee create mode 100644 src/content-views.coffee create mode 100644 src/iframe-content.coffee create mode 100644 src/iframe-replace.coffee diff --git a/assets/iframe-replace.html b/assets/iframe-replace.html new file mode 100644 index 0000000..39caf75 --- /dev/null +++ b/assets/iframe-replace.html @@ -0,0 +1,16 @@ + + + + + + + + + 禁これ + + +
+ + diff --git a/assets/manifest.json b/assets/manifest.json index fa4961e..b57d368 100644 --- a/assets/manifest.json +++ b/assets/manifest.json @@ -5,13 +5,18 @@ "description": "艦これのプレイ可能時間を制限します", "icons": {"128":"icon_128.png"}, "background": { - "scripts": ["background.js"], + "scripts": ["consts.js","lib.js","background.js"], "persistent": false }, "content_scripts": [ { "matches":["http://www.dmm.com/netgame/social/-/gadgets/=/app_id=854854/"], - "js":["content.js"] + "js":["content-messaging.js","content-views.js","content.js"] + }, + { + "matches":["http://osapi.dmm.com/gadgets/*"], + "js":["content-messaging.js","iframe-content.js"], + "run_at":"document_start" } ], "options_page":"options.html" diff --git a/assets/options.html b/assets/options.html index 6b8e7f3..e9edfad 100644 --- a/assets/options.html +++ b/assets/options.html @@ -5,6 +5,8 @@ 禁これ オプション + + diff --git a/src/background.coffee b/src/background.coffee index 41e36bd..c4d039a 100644 --- a/src/background.coffee +++ b/src/background.coffee @@ -1,16 +1,35 @@ -checkTimeForResponse = (req,sender,sendResponse)-> +checkTimeForResponse = (req,sender,sendResponse,option)-> + playable = Kincolle.Schedule.isDatePlayable(new Date()) + if option && option.from is "iframe" + Kincolle.Temp.set("iframe-url",option.url) + if Kincolle.Temp.get("playableOnce") + playable = true sendResponse type: "checkTime" - playable: Kincolle.Schedule.isDatePlayable(new Date()) + playable: playable config: Kincolle.Config.load() +playableOnceForResponse = (req,sender,sendResponse,option)-> + Kincolle.Temp.set('playableOnce',true) + sendResponse + type: "playableOnce" + +getIframeURLForResponse = (req,sender,sendResponse,option)-> + sendResponse + type: "getIFrameURL" + url: Kincolle.Temp.get("iframe-url") + chrome.runtime.onInstalled.addListener ()-> if !localStorage.schedule chrome.tabs.create({url:chrome.runtime.getURL("options.html")}) chrome.runtime.onMessage.addListener (req,sender,sendResponse)-> if req.type is "checkTime" - return checkTimeForResponse(req,sender,sendResponse) + return checkTimeForResponse(req,sender,sendResponse,req.option) + else if req.type is "playableOnce" + return playableOnceForResponse(req,sender,sendResponse,req.option) + else if req.type is "getIframeURL" + return getIframeURLForResponse(req,sender,sendResponse,req.option) else sendResponse type: "error" diff --git a/src/content-messaging.coffee b/src/content-messaging.coffee new file mode 100644 index 0000000..766340f --- /dev/null +++ b/src/content-messaging.coffee @@ -0,0 +1,23 @@ +window.Kincolle ||={} + +Kincolle.ContentMessaging ||={} +Kincolle.ContentMessaging.isPlayable = (playable,unplayable,option)-> + chrome.runtime.sendMessage {type:"checkTime",option:option},(res)-> + if res.playable + playable(res.config) + else + unplayable(res.config) + +Kincolle.ContentMessaging.getNextPeriod = (now)-> + next = new Date(now.getTime() + 60*60*1000) + next.setMilliseconds(0) + next.setSeconds(0) + next.setMinutes(0) + return next +Kincolle.ContentMessaging.playableOnce = (callback,option)-> + chrome.runtime.sendMessage {type:"playableOnce",option:option},(res)-> + callback(res.config) + +Kincolle.ContentMessaging.getIframeURL = (callback,option)-> + chrome.runtime.sendMessage {type:"getIframeURL",option:option},(res)-> + callback(res.url) diff --git a/src/content-views.coffee b/src/content-views.coffee new file mode 100644 index 0000000..cfddc12 --- /dev/null +++ b/src/content-views.coffee @@ -0,0 +1,40 @@ +window.Kincolle ||= {} +Kincolle.Views ||= {} + +Kincolle.Views.generatePlaceHolder =(w,h,config)-> + p = document.createElement('div') + s = p.style + s.width = "#{w}px" + s.height = "#{h}px" + s.background = "#000" + s.color = "#fff" + p.appendChild Kincolle.Views.generateMessage() + if(config && config.useGGKSMKK) + p.appendChild Kincolle.Views.generateIgnoreButton(config.click) + return p + +Kincolle.Views.generateMessage = -> + m = document.createElement('div') + m.textContent = "提督、今日はお休みではなかったのですか?" + s = m.style + s.font = "28px sans-serif" + s.textAlign = "center" + s.padding = "15px 0 10px" + return m + +Kincolle.Views.generateIgnoreButton = (click)-> + w = document.createElement('div') + w.style.textAlign = "center" + b = document.createElement('button') + w.appendChild(b) + b.innerText = "月月火水木金金" + if click + b.addEventListener 'click',click + s = b.style + s.padding = "8px" + s.background = "#000" + s.color = "#fff" + s.border = "1px solid #fff" + s.font = "16px sans-serif" + return w + diff --git a/src/content.coffee b/src/content.coffee index 5a1b9b6..a92d427 100644 --- a/src/content.coffee +++ b/src/content.coffee @@ -12,9 +12,9 @@ removeChildrenOfTree = (tree)-> hideGame = (config)-> visible = false tree = document.querySelector('#area-game') - width = tree.clientWidth - height = tree.clientHeight - placeHolder = generatePlaceHolder(width,height,config) + width = 916 + height = 610 + placeHolder = Kincolle.Views.generatePlaceHolder(width,height,config) treeContent = removeChildrenOfTree(tree) tree.appendChild(placeHolder) @@ -25,60 +25,21 @@ showGame = -> for c in treeContent tree.appendChild(c) -generatePlaceHolder = (w,h,config)-> - p = document.createElement('div') - s = p.style - s.width = "#{w}px" - #s.height = "#{h}px" - s.height = "610px" - s.background = "#000" - s.color = "#fff" - p.appendChild generateMessage() - if(config && config.useGGKSMKK) - p.appendChild generateIgnoreButton() - return p - -generateMessage = -> - m = document.createElement('div') - m.textContent = "提督、今日はお休みではなかったのですか?" - s = m.style - s.font = "28px sans-serif" - s.textAlign = "center" - s.padding = "15px 0 10px" - return m - -generateIgnoreButton = -> - w = document.createElement('div') - b = document.createElement('button') - w.appendChild(b) - b.innerText = "月月火水木金金" - b.addEventListener 'click',(e)-> - ggksmkk = true - showGame() - e.preventDefault() - return false - s = b.style - s.padding = "8px" - s.background = "#000" - s.color = "#fff" - s.border = "1px solid #fff" - s.font = "16px sans-serif" - return w - - ############### +playable = (config)-> + if(!visible) + showGame(config) +unplayable = (config)-> + if(visible && !ggksmkk) + config.click = (e)-> + ggksmkk = true + showGame() + e.preventDefault() + return false + hideGame(config) requestPlayable = ()-> - chrome.runtime.sendMessage {type:"checkTime"},(res)-> - if(visible && !res.playable && !ggksmkk) - hideGame(res.config) - else if(!visible && res.playable) - showGame(res.config) - now = new Date() - next = new Date(now.getTime() + 60*60*1000) - next.setMilliseconds(0) - next.setSeconds(0) - next.setMinutes(0) - setTimeout(requestPlayable,next-now) - #setTimeout(requestPlayable,60000) - + Kincolle.ContentMessaging.isPlayable playable,unplayable + now = new Date() + next = Kincolle.ContentMessaging.getNextPeriod(now) + setTimeout(requestPlayable,next-now) requestPlayable() diff --git a/src/iframe-content.coffee b/src/iframe-content.coffee new file mode 100644 index 0000000..48ad2ae --- /dev/null +++ b/src/iframe-content.coffee @@ -0,0 +1,6 @@ +unplayable = ()-> + window.location.href = chrome.runtime.getURL("iframe-replace.html") +playable = (()->) +Kincolle.ContentMessaging.isPlayable playable,unplayable, + from: "iframe" + url: location.href diff --git a/src/iframe-replace.coffee b/src/iframe-replace.coffee new file mode 100644 index 0000000..e475732 --- /dev/null +++ b/src/iframe-replace.coffee @@ -0,0 +1,33 @@ +ggksmkk = false + +reload = ()-> + Kincolle.ContentMessaging.getIframeURL (url)-> + location.href = url + +playable = (config)-> + reload() + +unplayable = (config)-> + config.click = (e)-> + Kincolle.ContentMessaging.playableOnce (config)-> + reload() + e.preventDefault() + return false + width = $(window).innerWidth() + height = $(window).innerHeight() + placeHolder = Kincolle.Views.generatePlaceHolder(width,height,config) + $('#area-game').append(placeHolder) + $(window).on 'resize',(e)-> + $(placeHolder).css + width : $(@).width() + height : $(@).height() + +requestPlayable = ()-> + Kincolle.ContentMessaging.isPlayable playable,unplayable + now = new Date() + next = Kincolle.ContentMessaging.getNextPeriod(now) + setTimeout(requestPlayable,next-now) + +$ -> + requestPlayable() + diff --git a/src/lib.coffee b/src/lib.coffee index 784b1d0..a85934f 100644 --- a/src/lib.coffee +++ b/src/lib.coffee @@ -1,7 +1,9 @@ window.Kincolle ||= {} +Function::property = (prop, desc) -> + Object.defineProperty @prototype, prop, desc -Kincolle.Schedule = {} +Kincolle.Schedule ||= {} Kincolle.Schedule.load = ()-> if localStorage.schedule return JSON.parse(localStorage.schedule) @@ -20,13 +22,12 @@ Kincolle.Schedule.isDatePlayable =(date)-> else return true -Kincolle.Config = {} +Kincolle.Config ||= {} Kincolle.Config.load = ()-> if localStorage.config return JSON.parse(localStorage.config) else return null - Kincolle.Config.save = (config)-> current = Kincolle.Config.load() if current @@ -34,3 +35,28 @@ Kincolle.Config.save = (config)-> current[key] = config[key] config = current localStorage.config = JSON.stringify(config) + +Kincolle.Temp ||= {} +loadTemp = ()-> + if localStorage.temp + return JSON.parse(localStorage.temp) + else + return null + +Kincolle.Temp.get = (key)-> + c = loadTemp() + v = null + if c + v = c[key] + delete(c[key]) + localStorage.temp = JSON.stringify(c) + return v + +Kincolle.Temp.set = (key,value)-> + c = loadTemp() + if !c + c = {} + c[key] = value + localStorage.temp = JSON.stringify(c) + +