// クリップボードにアンカー※のHTMLソースをコピーするスクリプト
// ※ <a href="url">文書タイトル等</a>
// 選択範囲があれば、<a href="url#id">選択範囲文字列(文書タイトル)</a>をコピー
// id は、選択範囲の要素のid属性、さもなくば親要素を遡及してid属性を探す
(function(){
// Sleipnirのみ ここから
var pnir = new ActiveXObject("Sleipnir.API");
var window = pnir.GetWindowObject(
pnir.GetDocumentID(pnir.ActiveIndex)
);
var document = window.document;
function alert(msg){ pnir.MessageBox(String(msg)); }
// Sleipnirのみ ここまで
/*******************/
/*** Class CDATA ***/
/*******************/
function CDATA(data){
this.pcdata = data;
var wrapper = CDATA.wrapperNode;
wrapper.appendChild(document.createTextNode(data));
this.cdata = wrapper.innerHTML;
wrapper.removeChild(wrapper.firstChild);
}
CDATA.wrapperNode = document.createElement('SPAN');
CDATA.prototype.toPCDATA = function(){ return this.pcdata; };
CDATA.prototype.valueOf = function(){ return this.cdata; };
CDATA.prototype.toString = function(){ return this.cdata; };
/*********************/
/*** Object Anchor ***/
/*********************/
function Anchor(){
var range = document.selection.createRange();
this.range = range;
this.OwnerNotFound = false;
var url = new CDATA(window.location.href);
var title = new CDATA(document.title);
this.heading = new CDATA(range.text);
try {
this.ownerNode = this.__getOwnerNode();
} catch(e) {
if (e instanceof OwnerNotFound)
this.ownerNode = undefined,
this.OwnerNotFound = true;
else
throw e;
}
this.urlref = this.ownerNode?
(url+'#'+this.ownerNode.id) : url;
this.text = this.ownerNode?
[range.text, '(', title, ')'].join('') : title;
}
Anchor.prototype.__getOwnerNode = function(){ // ..may throw OwnerNotFound
if (this.heading.toPCDATA() == '')
return undefined;
var elm = this.range.parentElement(), id = elm.id;
while(!Boolean(id)){
try {
elm = elm.parentNode;
id = elm.id;
} catch(e) {
throw new OwnerNotFound();
}
}
return elm;
};
Anchor.prototype.toString = function(){
return '<a href="'+this.urlref+'">'+this.text+'</a>';
};
/**********************/
/*** Object Message ***/
/**********************/
function Message(anc, bCopied){
if (!bCopied)
this["Status"] = 'Copy failed. Do it manually.';
else if (anc.OwnerNotFound)
this["Status"] = 'Copy Completed. But Expected ID Not Found.';
else
this["Status"] = 'Copy Completed Successfully.'
this["Anchor Text"] = anc.text;
this["URI Reference"] = anc.urlref;
if (anc.ownerNode)
this["ID"] = anc.ownerNode.id,
this["Parent Node"] = anc.ownerNode.tagName;
else
this["ID"] = 'Not Found',
this["Parent Node"] = 'Not Found';
if (bCopied)
this["Copied Text"] = anc.toString();
}
Message.prototype.toString = function(){
var ary = [];
for (var prop in this)
ary.push('<<'+prop+'>>\n '+this[prop]);
return ary.join('\n\n');
};
/***************************/
/*** Error OwnerNotFound ***/
/***************************/
function OwnerNotFound(message){
this.message = message;
}
/************************/
/*** Error CopyFailed ***/
/************************/
function CopyFailed(message){
this.message = message;
}
/*********************/
/*** Function copy ***/
/*********************/
function copy(str){ // ..may throw CopyFailed
str = String(str);
if (!window.clipboardData.setData('Text', str))
throw new CopyFailed(str);
/*
else {
var node = document.createElement('TEXTAREA');
node.appendChild( document.createTextNode(str) );
var rng = node.createTextRange();
var b = rng.execCommand("Copy");
node.removeChild(node.firstChild);
document.body.appendChild(node);
document.body.removeChild(node);
return b;
}
*/
}
/************/
/*** Main ***/
/************/
var anchor = new Anchor();
try {
copy(anchor);
alert(new Message(anchor, true));
} catch(e) {
if (e instanceof CopyFailed)
window.prompt(new Message(anchor, false), e.message);
else
throw e;
}
})();
// clipboardData Object:
// http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/clipboarddata.asp
SGMLをナメた類似品にご注意。
(function(){
// 製作者側のCSSの有効/無効を切り替える
// Sleipnirのみ ここから
var pnir = new ActiveXObject("Sleipnir.API");
var window = pnir.GetWindowObject(
pnir.GetDocumentID(pnir.ActiveIndex)
);
var document = window.document;
// Sleipnirのみ ここまで
(document.getElementsByTagName('FRAMESET').length !== 0) ?
function rootin(_frm)
{
for(var i = 0, len = _frm.length; i < len; i++)
{
var child = _frm(i);
var frmChild = child.frames;
try
{
(frmChild.length !== 0) ?
arguments.callee(frmChild) : toggle(child.document);
}
catch(e)
{
continue;
}
}
}(window.frames)
:
function(){
toggle(document);
rootin(window.frames);
}();
function toggle(_doc)
{
var ss = _doc.styleSheets;
for(var i = 0, s, l = ss.length; i < l; i++)
{
s = ss.item(i);
if(s.x === undefined)
{
s.x = s.disabled;
s.disabled = true;
}
else
{
s.disabled = s.x;
s.x = undefined;
}
}
}
})();
(function(){
// URIが示された引用元を明示する
// Sleipnirのみ ここから
var pnir = new ActiveXObject("Sleipnir.API");
var window = pnir.GetWindowObject(
pnir.GetDocumentID(pnir.ActiveIndex)
);
var document = window.document;
function alert(msg){ pnir.MessageBox(msg); }
// Sleipnirのみ ここまで
var nlBQs = document.getElementsByTagName('BLOCKQUOTE'), lenBQ=0;
var nlQs = document.getElementsByTagName('Q'), lenQ=0;
for(var i=0,len=nlBQs.length; i<len; i++) {
var elmBQ = nlBQs.item(i);
var cite = elmBQ.getAttribute('cite');
var title = elmBQ.getAttribute('title');
if (!cite && !title)
continue;
lenBQ++;
var elmP = document.createElement('P');
elmP.style.align = 'right';
var elmA = document.createElement('A');
if (cite)
elmA.setAttribute('href', cite);
elmA.appendChild(document.createTextNode(title? title : cite));
elmP.appendChild(elmA);
elmP.appendChild(document.createTextNode(' より'));
elmBQ.parentNode.insertBefore(elmP, elmBQ.nextSibling);
}
for (var i=0,len=nlQs.length; i<len; i++) {
var elmQ = nlQs.item(i);
var cite = elmQ.getAttribute('cite');
if (!cite && !title)
continue;
lenQ++;
var title = elmQ.getAttribute('title');
var elmA = document.createElement('A');
if (cite)
elmA.setAttribute('href', cite);
if (title)
elmA.setAttribute('title', title);
elmA.appendChild(document.createTextNode(' * '));
elmQ.parentNode.insertBefore(elmA, elmQ.nextSibling);
}
alert('Blockquote*'+lenBQ+', Q*'+lenQ);
})();