| 287 | | |
| | 291 | // Auto UserScript Updater |
| | 292 | function UpdateChecker() {}; |
| | 293 | UpdateChecker.prototype = { |
| | 294 | script_name: 'Nicomment Cloud', |
| | 295 | script_url: 'http://blog.fulltext-search.biz/files/nicommentcloud.user.js', |
| | 296 | current_version: '0.1.0', |
| | 297 | more_info_url: 'http://blog.fulltext-search.biz/pages/nicomment-cloud', |
| | 298 | |
| | 299 | remote_version: null, |
| | 300 | |
| | 301 | // Render update information in HTML |
| | 302 | render_update_info: function() { |
| | 303 | var self = this; |
| | 304 | var newversion = document.createElement('div'); |
| | 305 | newversion.setAttribute('id', 'gm_update_alert'); |
| | 306 | var update_message = document.createElement('p'); |
| | 307 | update_message.innerHTML = [ |
| | 308 | '現在お使いのGreasemonkeyスクリプト \'', |
| | 309 | this.script_name, |
| | 310 | '(var. ', this.current_version, ')', |
| | 311 | '\' は新しいバージョン ', |
| | 312 | this.remote_version, |
| | 313 | ' が公開されています.アップデートしますか?' |
| | 314 | ].join(''); |
| | 315 | |
| | 316 | var update_link = document.createElement('a'); |
| | 317 | update_link.setAttribute('id', 'gm_update_alert_link'); |
| | 318 | update_link.setAttribute('href', this.script_url); |
| | 319 | update_link.addEventListener('click', function() { |
| | 320 | var update_alert = document.getElementById('gm_update_alert'); |
| | 321 | update_alert.parentNode.removeChild(update_alert); |
| | 322 | }, false); |
| | 323 | update_link.innerHTML = |
| | 324 | '[Yes]今すぐアップデートする'; |
| | 325 | |
| | 326 | if(this.more_info_url) { |
| | 327 | var more_link = document.createElement('a'); |
| | 328 | more_link.setAttribute('href', this.more_info_url); |
| | 329 | more_link.innerHTML = '(詳細情報)'; |
| | 330 | update_message.appendChild(more_link); |
| | 331 | } |
| | 332 | |
| | 333 | var close_link = document.createElement('a'); |
| | 334 | close_link.setAttribute('href', 'javascript:void(0);'); |
| | 335 | close_link.addEventListener('click', function() { |
| | 336 | GM_setValue('last_check_day', self.days_since_start()); |
| | 337 | var update_alert = document.getElementById('gm_update_alert'); |
| | 338 | update_alert.parentNode.removeChild(update_alert); |
| | 339 | }, false); |
| | 340 | close_link.innerHTML = [ |
| | 341 | '[No]今はアップデートしない(日付が変わるまで有効)' |
| | 342 | ].join(''); |
| | 343 | |
| | 344 | var scroll_link = document.createElement('a'); |
| | 345 | if(/(.*?)\#/.test(document.location)) { |
| | 346 | scroll_link.href = RegExp.$1 + '#gm_update_alert'; |
| | 347 | } else { |
| | 348 | scroll_link.href = document.location + '#gm_update_alert'; |
| | 349 | } |
| | 350 | scroll_link.innerHTML = '(ページ最上部に"' + this.script_name + '"からのお知らせがあります)'; |
| | 351 | |
| | 352 | newversion.appendChild(update_message); |
| | 353 | newversion.appendChild(update_link); |
| | 354 | newversion.appendChild(close_link); |
| | 355 | document.body.appendChild(newversion); |
| | 356 | $('WATCHFOOTER').parentNode.insertBefore(scroll_link, $('WATCHFOOTER')); |
| | 357 | }, |
| | 358 | |
| | 359 | add_update_info_style: function() { |
| | 360 | GM_addStyle(<><![CDATA[ |
| | 361 | /* style(like CSS) for update information */ |
| | 362 | #gm_update_alert { |
| | 363 | padding: 5px 0pt; |
| | 364 | background-color: #FFF280; |
| | 365 | color: #CC0099; |
| | 366 | width: 100%; |
| | 367 | position: absolute; |
| | 368 | z-index: 99; |
| | 369 | top: 0px; |
| | 370 | left: 0px; |
| | 371 | text-align: center; |
| | 372 | font-size: 11px; |
| | 373 | font-family: Tahoma; |
| | 374 | } |
| | 375 | |
| | 376 | #gm_update_alert p { |
| | 377 | margin: 0pt; |
| | 378 | } |
| | 379 | |
| | 380 | #gm_update_alert a:link { |
| | 381 | color: #333333; |
| | 382 | } |
| | 383 | |
| | 384 | #gm_update_alert > a:link { |
| | 385 | margin: 0.5em 1em 0pt 1em; |
| | 386 | } |
| | 387 | |
| | 388 | #gm_update_alert p + a:link { |
| | 389 | font-weight: bold; |
| | 390 | } |
| | 391 | ]]></>); |
| | 392 | }, |
| | 393 | |
| | 394 | // Check script update remote |
| | 395 | check_update: function() { |
| | 396 | if(!this.has_need_for_check) return; |
| | 397 | var user_script = this; |
| | 398 | GM_xmlhttpRequest({ |
| | 399 | method: 'GET', |
| | 400 | url: this.script_url, |
| | 401 | onload: function(res) { |
| | 402 | user_script.remote_version = user_script.check_version(res.responseText); |
| | 403 | if(user_script.remote_version && user_script.remote_version > user_script.current_version) { |
| | 404 | user_script.add_update_info_style(); |
| | 405 | user_script.render_update_info(); |
| | 406 | } else { |
| | 407 | GM_setValue('last_check_day', user_script.days_since_start()); |
| | 408 | } |
| | 409 | }, |
| | 410 | onerror: function(res) { GM_log(res.status + ':' + res.responseText); } |
| | 411 | }); |
| | 412 | }, |
| | 413 | |
| | 414 | // Check the necessity for update: [Boolean] |
| | 415 | // return [true] if necessary |
| | 416 | has_need_for_check: function() { |
| | 417 | var last_check_day = GM_getValue('last_check_day'); |
| | 418 | var current_day = this.days_since_start(); |
| | 419 | if(typeof last_check_day == 'undefined' || current_day > last_check_day) { |
| | 420 | return true; |
| | 421 | } else { |
| | 422 | return false; |
| | 423 | } |
| | 424 | }, |
| | 425 | |
| | 426 | // Check version in remote script file: [String] |
| | 427 | check_version: function(string) { |
| | 428 | if(/\/\/\s?@version\s+([\d.]+)/.test(string)) { |
| | 429 | return RegExp.$1; |
| | 430 | } else { |
| | 431 | return null; |
| | 432 | } |
| | 433 | }, |
| | 434 | |
| | 435 | days_since_start: function() { |
| | 436 | var DAYS_IN_MONTH = [31,59,90,120,151,181,212,243,273,304,334,365]; |
| | 437 | var now = new Date(); |
| | 438 | return(now.getYear() * 365 + DAYS_IN_MONTH[now.getMonth()] + now.getDate()); |
| | 439 | } |
| | 440 | }; |
| | 441 | |
| | 442 | var user_script = new UpdateChecker(); |
| | 443 | user_script.check_update(); |