Support refreshing 4chan threads in Thread Watcher from other sites.
This commit is contained in:
parent
ad16e13b61
commit
4c139b3b0f
@ -24,8 +24,6 @@ ThreadWatcher =
|
|||||||
$.on @refreshButton, 'click', @buttonFetchAll
|
$.on @refreshButton, 'click', @buttonFetchAll
|
||||||
$.on @closeButton, 'click', @toggleWatcher
|
$.on @closeButton, 'click', @toggleWatcher
|
||||||
|
|
||||||
@refreshButton.hidden = true unless Site.software is 'yotsuba'
|
|
||||||
|
|
||||||
@menu.addHeaderMenuEntry()
|
@menu.addHeaderMenuEntry()
|
||||||
$.onExists doc, 'body', @addDialog
|
$.onExists doc, 'body', @addDialog
|
||||||
|
|
||||||
@ -184,13 +182,12 @@ ThreadWatcher =
|
|||||||
ThreadWatcher.clearRequests()
|
ThreadWatcher.clearRequests()
|
||||||
|
|
||||||
fetchAuto: ->
|
fetchAuto: ->
|
||||||
return unless Site.software is 'yotsuba'
|
|
||||||
clearTimeout ThreadWatcher.timeout
|
clearTimeout ThreadWatcher.timeout
|
||||||
return unless Conf['Auto Update Thread Watcher']
|
return unless Conf['Auto Update Thread Watcher']
|
||||||
{db} = ThreadWatcher
|
{db} = ThreadWatcher
|
||||||
interval = if ThreadWatcher.unreadEnabled and Conf['Show Unread Count'] then 5 * $.MINUTE else 2 * $.HOUR
|
interval = if ThreadWatcher.unreadEnabled and Conf['Show Unread Count'] then 5 * $.MINUTE else 2 * $.HOUR
|
||||||
now = Date.now()
|
now = Date.now()
|
||||||
unless now - interval < (db.data[Site.hostname].lastChecked or 0) <= now or d.hidden or not d.hasFocus()
|
unless now - interval < (db.data.lastChecked or 0) <= now or d.hidden or not d.hasFocus()
|
||||||
ThreadWatcher.fetchAllStatus()
|
ThreadWatcher.fetchAllStatus()
|
||||||
db.setLastChecked()
|
db.setLastChecked()
|
||||||
ThreadWatcher.timeout = setTimeout ThreadWatcher.fetchAuto, interval
|
ThreadWatcher.timeout = setTimeout ThreadWatcher.fetchAuto, interval
|
||||||
@ -202,7 +199,6 @@ ThreadWatcher =
|
|||||||
ThreadWatcher.fetchAllStatus()
|
ThreadWatcher.fetchAllStatus()
|
||||||
|
|
||||||
fetchAllStatus: ->
|
fetchAllStatus: ->
|
||||||
return unless Site.software is 'yotsuba'
|
|
||||||
dbs = [ThreadWatcher.db, ThreadWatcher.unreaddb, QuoteYou.db].filter((x) -> x)
|
dbs = [ThreadWatcher.db, ThreadWatcher.unreaddb, QuoteYou.db].filter((x) -> x)
|
||||||
n = 0
|
n = 0
|
||||||
for db in dbs
|
for db in dbs
|
||||||
@ -215,18 +211,26 @@ ThreadWatcher =
|
|||||||
|
|
||||||
fetchStatus: (thread, force) ->
|
fetchStatus: (thread, force) ->
|
||||||
{siteID, boardID, threadID, data} = thread
|
{siteID, boardID, threadID, data} = thread
|
||||||
return unless Site.software is 'yotsuba' and siteID is Site.hostname
|
return unless Conf['siteProperties'][siteID]?.software is 'yotsuba'
|
||||||
return if data.isDead and not force
|
return if data.isDead and not force
|
||||||
if ThreadWatcher.requests.length is 0
|
if ThreadWatcher.requests.length is 0
|
||||||
ThreadWatcher.status.textContent = '...'
|
ThreadWatcher.status.textContent = '...'
|
||||||
$.addClass ThreadWatcher.refreshButton, 'fa-spin'
|
$.addClass ThreadWatcher.refreshButton, 'fa-spin'
|
||||||
req = $.ajax "#{location.protocol}//a.4cdn.org/#{boardID}/thread/#{threadID}.json",
|
url = "#{location.protocol}//a.4cdn.org/#{boardID}/thread/#{threadID}.json"
|
||||||
onloadend: ->
|
if Site.hasCORS?(url) or url.split('/')[...3].join('/') is location.origin
|
||||||
|
req = $.ajax url,
|
||||||
|
onloadend: ->
|
||||||
|
ThreadWatcher.parseStatus.call @, thread
|
||||||
|
timeout: $.MINUTE
|
||||||
|
else
|
||||||
|
req = {abort: () -> req.aborted = true}
|
||||||
|
CrossOrigin.json url, ->
|
||||||
|
return if req.aborted
|
||||||
ThreadWatcher.parseStatus.call @, thread
|
ThreadWatcher.parseStatus.call @, thread
|
||||||
timeout: $.MINUTE
|
, true, $.MINUTE
|
||||||
ThreadWatcher.requests.push req
|
ThreadWatcher.requests.push req
|
||||||
|
|
||||||
parseStatus: ({boardID, threadID, data}) ->
|
parseStatus: ({siteID, boardID, threadID, data}) ->
|
||||||
ThreadWatcher.fetched++
|
ThreadWatcher.fetched++
|
||||||
if ThreadWatcher.fetched is ThreadWatcher.requests.length
|
if ThreadWatcher.fetched is ThreadWatcher.requests.length
|
||||||
ThreadWatcher.clearRequests()
|
ThreadWatcher.clearRequests()
|
||||||
@ -237,24 +241,20 @@ ThreadWatcher =
|
|||||||
last = @response.posts[@response.posts.length-1].no
|
last = @response.posts[@response.posts.length-1].no
|
||||||
isDead = !!@response.posts[0].archived
|
isDead = !!@response.posts[0].archived
|
||||||
if isDead and Conf['Auto Prune']
|
if isDead and Conf['Auto Prune']
|
||||||
ThreadWatcher.db.delete {boardID, threadID}
|
ThreadWatcher.db.delete {siteID, boardID, threadID}
|
||||||
ThreadWatcher.refresh()
|
ThreadWatcher.refresh()
|
||||||
return
|
return
|
||||||
|
|
||||||
return if last is data.last and isDead is data.isDead
|
return if last is data.last and isDead is data.isDead
|
||||||
|
|
||||||
lastReadPost = ThreadWatcher.unreaddb.get
|
lastReadPost = ThreadWatcher.unreaddb.get {siteID, boardID, threadID, defaultValue: 0}
|
||||||
boardID: boardID
|
|
||||||
threadID: threadID
|
|
||||||
defaultValue: 0
|
|
||||||
|
|
||||||
unread = 0
|
unread = 0
|
||||||
quotingYou = false
|
quotingYou = false
|
||||||
youOP = !!QuoteYou.db?.get {boardID, threadID, postID: threadID}
|
youOP = !!QuoteYou.db?.get {siteID, boardID, threadID, postID: threadID}
|
||||||
|
|
||||||
for postObj in @response.posts
|
for postObj in @response.posts
|
||||||
continue unless postObj.no > lastReadPost
|
continue unless postObj.no > lastReadPost
|
||||||
continue if QuoteYou.db?.get {boardID, threadID, postID: postObj.no}
|
continue if QuoteYou.db?.get {siteID, boardID, threadID, postID: postObj.no}
|
||||||
|
|
||||||
unread++
|
unread++
|
||||||
|
|
||||||
@ -268,6 +268,7 @@ ThreadWatcher =
|
|||||||
regexp = /<a [^>]*\bhref="(?:(?:\/\/boards\.4chan(?:nel)?\.org)?\/([^\/]+)\/thread\/)?(\d+)?(?:#p(\d+))?"/g
|
regexp = /<a [^>]*\bhref="(?:(?:\/\/boards\.4chan(?:nel)?\.org)?\/([^\/]+)\/thread\/)?(\d+)?(?:#p(\d+))?"/g
|
||||||
while match = regexp.exec postObj.com
|
while match = regexp.exec postObj.com
|
||||||
if QuoteYou.db.get {
|
if QuoteYou.db.get {
|
||||||
|
siteID
|
||||||
boardID: match[1] or boardID
|
boardID: match[1] or boardID
|
||||||
threadID: match[2] or threadID
|
threadID: match[2] or threadID
|
||||||
postID: match[3] or match[2] or threadID
|
postID: match[3] or match[2] or threadID
|
||||||
@ -278,14 +279,14 @@ ThreadWatcher =
|
|||||||
quotingYou = true
|
quotingYou = true
|
||||||
|
|
||||||
updated = (isDead isnt data.isDead or unread isnt data.unread or quotingYou isnt data.quotingYou)
|
updated = (isDead isnt data.isDead or unread isnt data.unread or quotingYou isnt data.quotingYou)
|
||||||
ThreadWatcher.db.extend {boardID, threadID, val: {last, isDead, unread, quotingYou}}
|
ThreadWatcher.db.extend {siteID, boardID, threadID, val: {last, isDead, unread, quotingYou}}
|
||||||
ThreadWatcher.refresh() if updated
|
ThreadWatcher.refresh() if updated
|
||||||
|
|
||||||
else if @status is 404
|
else if @status is 404
|
||||||
if Conf['Auto Prune']
|
if Conf['Auto Prune']
|
||||||
ThreadWatcher.db.delete {boardID, threadID}
|
ThreadWatcher.db.delete {siteID, boardID, threadID}
|
||||||
else
|
else
|
||||||
ThreadWatcher.db.extend {boardID, threadID, val: {isDead: true}, rm: ['unread', 'quotingYou']}
|
ThreadWatcher.db.extend {siteID, boardID, threadID, val: {isDead: true}, rm: ['unread', 'quotingYou']}
|
||||||
|
|
||||||
ThreadWatcher.refresh()
|
ThreadWatcher.refresh()
|
||||||
|
|
||||||
|
|||||||
@ -90,9 +90,9 @@ class DataBoard
|
|||||||
@setUnsafe {siteID, boardID, threadID, postID, val: oldVal}
|
@setUnsafe {siteID, boardID, threadID, postID, val: oldVal}
|
||||||
, cb
|
, cb
|
||||||
|
|
||||||
setLastChecked: (siteID=Site.hostname) ->
|
setLastChecked: ->
|
||||||
@save =>
|
@save =>
|
||||||
@data[siteID].lastChecked = Date.now()
|
@data.lastChecked = Date.now()
|
||||||
|
|
||||||
get: ({siteID, boardID, threadID, postID, defaultValue}) ->
|
get: ({siteID, boardID, threadID, postID, defaultValue}) ->
|
||||||
siteID or= Site.hostname
|
siteID or= Site.hostname
|
||||||
|
|||||||
@ -1174,6 +1174,9 @@ span.hide-announcement {
|
|||||||
-webkit-flex: 0 1 auto;
|
-webkit-flex: 0 1 auto;
|
||||||
flex: 0 1 auto;
|
flex: 0 1 auto;
|
||||||
}
|
}
|
||||||
|
.replies-quoting-you > a, #watcher-link.replies-quoting-you {
|
||||||
|
color: #F00;
|
||||||
|
}
|
||||||
#thread-watcher a {
|
#thread-watcher a {
|
||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -26,6 +26,7 @@ ajax = (request, sender, sendResponse) ->
|
|||||||
xhr = new XMLHttpRequest()
|
xhr = new XMLHttpRequest()
|
||||||
xhr.open 'GET', request.url, true
|
xhr.open 'GET', request.url, true
|
||||||
xhr.responseType = request.responseType
|
xhr.responseType = request.responseType
|
||||||
|
xhr.timeout = request.timeout
|
||||||
xhr.addEventListener 'load', ->
|
xhr.addEventListener 'load', ->
|
||||||
{status, statusText, response} = @
|
{status, statusText, response} = @
|
||||||
if @readyState is @DONE && xhr.status is 200
|
if @readyState is @DONE && xhr.status is 200
|
||||||
@ -43,4 +44,7 @@ ajax = (request, sender, sendResponse) ->
|
|||||||
xhr.addEventListener 'abort', ->
|
xhr.addEventListener 'abort', ->
|
||||||
chrome.tabs.sendMessage sender.tab.id, {id, error: true}
|
chrome.tabs.sendMessage sender.tab.id, {id, error: true}
|
||||||
, false
|
, false
|
||||||
|
xhr.addEventListener 'timeout', ->
|
||||||
|
chrome.tabs.sendMessage sender.tab.id, {id, error: true}
|
||||||
|
, false
|
||||||
xhr.send()
|
xhr.send()
|
||||||
|
|||||||
@ -4,8 +4,8 @@ eventPageRequest = do ->
|
|||||||
chrome.runtime.onMessage.addListener (data) ->
|
chrome.runtime.onMessage.addListener (data) ->
|
||||||
callbacks[data.id] data
|
callbacks[data.id] data
|
||||||
delete callbacks[data.id]
|
delete callbacks[data.id]
|
||||||
(url, responseType, cb) ->
|
(url, responseType, cb, timeout) ->
|
||||||
chrome.runtime.sendMessage {url, responseType}, (id) ->
|
chrome.runtime.sendMessage {url, responseType, timeout}, (id) ->
|
||||||
callbacks[id] = cb
|
callbacks[id] = cb
|
||||||
|
|
||||||
<% } %>
|
<% } %>
|
||||||
@ -85,7 +85,7 @@ CrossOrigin =
|
|||||||
$.queueTask -> cb.call {}
|
$.queueTask -> cb.call {}
|
||||||
delete callbacks[url]
|
delete callbacks[url]
|
||||||
|
|
||||||
(url, cb, bypassCache) ->
|
(url, cb, bypassCache, timeout) ->
|
||||||
<% if (type === 'userscript') { %>
|
<% if (type === 'userscript') { %>
|
||||||
unless GM?.xmlHttpRequest? or GM_xmlhttpRequest?
|
unless GM?.xmlHttpRequest? or GM_xmlhttpRequest?
|
||||||
if bypassCache
|
if bypassCache
|
||||||
@ -99,18 +99,20 @@ CrossOrigin =
|
|||||||
|
|
||||||
if bypassCache
|
if bypassCache
|
||||||
delete results[url]
|
delete results[url]
|
||||||
if results[url]
|
else
|
||||||
cb.call results[url]
|
if results[url]
|
||||||
return
|
cb.call results[url]
|
||||||
if callbacks[url]
|
return
|
||||||
callbacks[url].push cb
|
if callbacks[url]
|
||||||
return
|
callbacks[url].push cb
|
||||||
|
return
|
||||||
callbacks[url] = [cb]
|
callbacks[url] = [cb]
|
||||||
|
|
||||||
<% if (type === 'userscript') { %>
|
<% if (type === 'userscript') { %>
|
||||||
(GM?.xmlHttpRequest or GM_xmlhttpRequest)
|
(GM?.xmlHttpRequest or GM_xmlhttpRequest)
|
||||||
method: "GET"
|
method: "GET"
|
||||||
url: url+''
|
url: url+''
|
||||||
|
timeout: timeout
|
||||||
onload: (xhr) ->
|
onload: (xhr) ->
|
||||||
{status, statusText} = xhr
|
{status, statusText} = xhr
|
||||||
try
|
try
|
||||||
@ -120,6 +122,7 @@ CrossOrigin =
|
|||||||
failure url
|
failure url
|
||||||
onerror: -> failure(url)
|
onerror: -> failure(url)
|
||||||
onabort: -> failure(url)
|
onabort: -> failure(url)
|
||||||
|
ontimeout: -> failure(url)
|
||||||
<% } %>
|
<% } %>
|
||||||
<% if (type === 'crx') { %>
|
<% if (type === 'crx') { %>
|
||||||
eventPageRequest url, 'json', (result) ->
|
eventPageRequest url, 'json', (result) ->
|
||||||
@ -127,4 +130,5 @@ CrossOrigin =
|
|||||||
success url, result
|
success url, result
|
||||||
else
|
else
|
||||||
failure url
|
failure url
|
||||||
|
, timeout
|
||||||
<% } %>
|
<% } %>
|
||||||
|
|||||||
@ -140,3 +140,6 @@ SW.yotsuba =
|
|||||||
for node in $$ '.prettyprint', bq
|
for node in $$ '.prettyprint', bq
|
||||||
$.replace node, [$.tn('[code]'), node.childNodes..., $.tn '[/code]']
|
$.replace node, [$.tn('[code]'), node.childNodes..., $.tn '[/code]']
|
||||||
return
|
return
|
||||||
|
|
||||||
|
hasCORS: (url) ->
|
||||||
|
url.split('/')[...3].join('/') is location.protocol + '//a.4cdn.org'
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user