Created
March 30, 2010 04:23
-
-
Save xulapp/348749 to your computer and use it in GitHub Desktop.
lastModifiedPanel.uc.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name lastModifiedPanel.uc.js | |
// @description adds toolbaritem that indicates Last-Modified. | |
// @include main | |
// @compatibility Firefox 4.0+ | |
// @namespace http://twitter.com/xulapp | |
// @author xulapp | |
// @license MIT License | |
// @version 2011/04/03 19:00 +09:00 addon-bar | |
// ==/UserScript== | |
// @version 2010/04/01 23:20 +09:00 閾値を大きくしてみた。ついでに 3.7 に対応 | |
// @version 2010/03/30 13:20 +09:00 | |
let lastModifiedPanel = (function() { | |
// 現在時刻との差がこの値未満なら Last-Modified ではないと判断する | |
const LOAD_TIME_THRESHOLD = 30 * 1000; | |
addStyle(<![CDATA[ | |
#lastModifiedPanel { | |
-moz-box-align: center; | |
} | |
#lastModifiedPanel.then > label { | |
opacity: 0.5; | |
} | |
#lastModifiedPanel.none > label { | |
opacity: 0; | |
} | |
]]>); | |
var item = $E(<toolbaritem id="lastModifiedPanel"/>); | |
var label = $E(<label/>); | |
$('addon-bar').appendChild(item).appendChild(label); | |
eval('gBrowser.updateTitlebar = ' + gBrowser.updateTitlebar.toString().replace( | |
/(?=\}$)/, | |
'lastModifiedPanel.updateStatusFor(this.contentDocument);' | |
)); | |
return { | |
updateStatusFor: updateStatusFor, | |
}; | |
function updateStatusFor(doc) { | |
if (!('SLMIS_state' in doc)) { | |
let lm = new Date(doc.lastModified); | |
if (lm.getFullYear() === 1970) { | |
item.classList.add('none'); | |
return; | |
} | |
item.classList.remove('none'); | |
doc.SLMIS_state = { | |
lastModified: lm.toLocaleString(), | |
then: Date.now() - lm < LOAD_TIME_THRESHOLD, | |
}; | |
} | |
var {lastModified, then} = doc.SLMIS_state; | |
if (then) { | |
item.classList.add('then'); | |
} else { | |
item.classList.remove('then'); | |
} | |
label.setAttribute('value', lastModified); | |
} | |
function $(id) document.getElementById(id); | |
function $E(xml, doc) { | |
doc = doc || document; | |
xml = <root xmlns={doc.documentElement.namespaceURI}>{xml}</root>; | |
var pp = XML.prettyPrinting; | |
XML.prettyPrinting = false; | |
var root = new DOMParser().parseFromString(xml.toXMLString(), 'application/xml').documentElement; | |
XML.prettyPrinting = pp; | |
doc.adoptNode(root); | |
var range = doc.createRange(); | |
range.selectNodeContents(root); | |
var frag = range.extractContents(); | |
range.detach(); | |
return frag.childNodes.length < 2 ? frag.firstChild : frag; | |
} | |
function addStyle(css) { | |
var pi = document.createProcessingInstruction( | |
'xml-stylesheet', | |
'type="text/css" href="data:text/css;utf-8,' + encodeURIComponent(css) + '"' | |
); | |
return document.insertBefore(pi, document.documentElement); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment