Hatena::Groupvimperator

toyo465の日記

2011-05-21tweetの内容をコピーするには

copy.jsに自分でテンプレートを追加できずに詰まってしまっているので、ここに書いてじっとコメントを待ってみたい。

:copyコマンドの補完一覧には出るものの、何もコピーされない。

js <<EOM
liberator.globalVariables.copy_templates = [
    { label: 'twitterText',    value: 'copy twitter',
        custom: function(){
            var twitterID = document.evaluate('/html/body/div/div[2]/div/div/div/div[2]/div/div/div/div/div/a',document,null,7,null).snapshotItem(0).innerHTML;
            var tweetText = document.evaluate('/html/body/div/div[2]/div/div/div/div[2]/div/div/div[2]/div',document,null,7,null).snapshotItem(0).innerHTML;
            var tweetTime = document.evaluate('/html/body/div/div[2]/div/div/div/div[2]/div/div/div[4]/a',document,null,7,null).snapshotItem(0).getAttribute("title");
            var result = twitterID + "\n" + tweetText + "\n" + tweetTime;
            return result;
        }
    }
];
EOM

追記

2011-05-23 03:09

>>id:vimp_fun

コメントありがとうございます。

・documentオブジェクトはchromeコンテキストのオブジェクトであって、コンテンツのdocumentではない

・コンテンツのドキュメントは "var contents=gBrowser.selectedBrowser.contentDocument;" などとして取得する。Vimeperatorではもう少し楽にコンテンツドキュメントを取得できるはず。

これは盲点でした。

firebugのコンソールではdocumentオブジェクトでalertが動作していたのでどうも気付けませんでした。代わりにutil.evaluateXPath()を使用することによって、期待通りの動作を実装できました。

また、旧UI/新UIではご指摘の通りXPathが異なりました。多くのことを学べました。vimp_funさん、teramakoさんに感謝。


UI
js <<EOM
liberator.globalVariables.copy_templates = [
    { label: 'twitterText',    value: 'copy twitter(old UI)',
        custom: function(){
            var xPathID = '//div[@id="permalink"]/div[2]/div[2]/a';
            var xPathText = '//div[@id="permalink"]/div[1]/span/span[1]/span[2]';
            var xPathTime = '//div[@id="permalink"]/div[1]/span/span[2]/a/span';
            var twitterID = util.evaluateXPath(xPathID,content.document).snapshotItem(0).textContent;
            var tweetText = util.evaluateXPath(xPathText,content.document).snapshotItem(0).textContent;
            var tweetTime = util.evaluateXPath(xPathTime,content.document).snapshotItem(0).getAttribute("data");
            var result = twitterID + "\n" + tweetText + "\n" + tweetTime;
            return result;
        }
    }
];
EOM
UI
js <<EOM
liberator.globalVariables.copy_templates = [
    { label: 'twitterText',    value: 'copy twitter(new UI)',
        custom: function(){
            var xPathID = '//div[@id="page-container"]/div/div/div[2]/div/div/div/div/div/a';
            var xPathText = '//div[@id="page-container"]/div/div/div[2]/div/div/div[2]/div';
            var xPathTime = '//div[@id="page-container"]/div/div/div[2]/div/div/div[4]/a';
            var twitterID = util.evaluateXPath(xPathID,content.document).snapshotItem(0).textContent;
            var tweetText = util.evaluateXPath(xPathText,content.document).snapshotItem(0).textContent;
            var tweetTime = util.evaluateXPath(xPathTime,content.document).snapshotItem(0).getAttribute("title");
            var result = twitterID + "\n" + tweetText + "\n" + tweetTime;
            return result;
        }
    }
];
EOM

vimp_funvimp_fun2011/05/22 15:38http://twitter.com/moscript/status/72167332037935104
を旧UIで表示している状態で

(function(){var contents=gBrowser.selectedBrowser.contentDocument;var xPath='//div[@id="permalink"]/div[2]/div[2]/a';var twitterID = contents.evaluate(xPath,contents,null,XPathResult.STRING_TYPE,null).stringValue;alert(twitterID);})();

をjsコマンドで実行させると moscript と alert されました。
コードを整形すると以下のようになります。

(function(){
var contents=gBrowser.selectedBrowser.contentDocument;
var xPath='//div[@id="permalink"]/div[2]/div[2]/a';
var twitterID = contents.evaluate(xPath,contents,null,XPathResult.STRING_TYPE,null).stringValue;
alert(twitterID);
})();

いくつかポイントがあって
・twitterのUIが新と旧ではおそらく記述すべきXPathが異なる
・XPathを記述する際、id 等をうまく利用するとシンプルに記述できる
・Dom Inspector偉い!!( Web Inspector でもいいのかも )
・documentオブジェクトはchromeコンテキストのオブジェクトであって、コンテンツのdocumentではない
・コンテンツのドキュメントは "var contents=gBrowser.selectedBrowser.contentDocument;" などとして取得する。Vimeperatorではもう少し楽にコンテンツドキュメントを取得できるはず。

vimp_funvimp_fun2011/05/22 17:34twitterで @teramako さんにさらにヒントをいただきました。下のようにも書けるようです。
util.evaluateXPath()はVimperatorのメソッドになります。戻り値はdocument.evaluate()と同じもののようです。

(function(){
var xPath='//div[@id="permalink"]/div[2]/div[2]/a';
var twitterID=util.evaluateXPath(xPath,content.document).snapshotItem(0).textContent;
alert(twitterID);
})();

参考:
Introduction to using XPath in JavaScript - MDC Docs
https://developer.mozilla.org/Ja/Introduction_to_using_XPath_in_JavaScript

LorrenLorren2011/11/22 01:23Extremely helpful article, plaese write more.

usuhqzittxusuhqzittx2011/11/22 17:43ffjEJH <a href="http://qboxtbvqkxcr.com/">qboxtbvqkxcr</a>

kvllzkokvllzko2011/11/23 02:344hfkKr , [url=http://uanwluzahibv.com/]uanwluzahibv[/url], [link=http://mpscjkoyfonh.com/]mpscjkoyfonh[/link], http://xqoyneyozuag.com/

bhdkscecjbbhdkscecjb2011/11/30 18:55vEaIcJ <a href="http://jhpgdygggviy.com/">jhpgdygggviy</a>

hkwgwfkxzphkwgwfkxzp2011/12/04 04:30Rgy4mO , [url=http://iotaniberkvr.com/]iotaniberkvr[/url], [link=http://ybsadpcfzqxh.com/]ybsadpcfzqxh[/link], http://aytqfxdodiii.com/