-
-
Save syoichi/3232139 to your computer and use it in GitHub Desktop.
リストの表示モードに対応した。
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 sumally-add-via-link | |
// @namespace swdyh | |
// @version 0.0.2 | |
// @description http://swdyh.tumblr.com/post/28474643309/sumally | |
// @include http://sumally.com/* | |
// @exclude http://sumally.com/p/* | |
// @run-at document-end | |
// ==/UserScript== | |
/* User Script info | |
date: 2012-08-02T10:11:32.782+9:00 | |
confirmed: | |
Windows 7 Home Premium SP1 64bit: | |
Firefox 14.0.1 | |
Google Chrome 21.0.1180.60 | |
*/ | |
/*jslint browser: true, maxlen: 80*/ | |
// Edition 2012-07-27 | |
(function executeAddViaLink(each, win, doc) { | |
'use strict'; | |
var listArea, range, selector, addVia, eachAddedNodes, recordsReader, MO; | |
listArea = doc.querySelector('.listLiquidArea, .listLineArea'); | |
if (!listArea) { | |
return; | |
} | |
doc.head.appendChild(doc.createElement('style')).textContent = [ | |
'a.via {', | |
' color: #F8A41A !important;', | |
' margin-left: 5px !important;', | |
'}', | |
'.name > a.via[target] {', | |
' margin-left: 0 !important;', | |
'}' | |
].join('\n'); | |
range = doc.createRange(); | |
selector = [ | |
'.text a[href^="http://sumally.com/p/"]', | |
'.name > a.p[href^="http://sumally.com/p/"]' | |
].join(','); | |
addVia = function addVia(node) { | |
var nodeClassList, itemLink; | |
nodeClassList = node.classList; | |
if ( | |
!( | |
nodeClassList && ( | |
nodeClassList.contains('oneblock') || | |
nodeClassList.contains('recoTimeline') | |
) | |
) | |
) { | |
return; | |
} | |
itemLink = node.querySelector(selector); | |
if (!(itemLink && !node.querySelector('.via'))) { | |
return; | |
} | |
itemLink.parentNode.insertAdjacentHTML( | |
'BeforeEnd', | |
'<a class="via" data-item-url="' + itemLink.href + '">via</a>' | |
); | |
}; | |
each.call(doc.querySelectorAll('.oneblock, .recoTimeline'), addVia); | |
range.selectNodeContents(doc.body); | |
listArea.addEventListener('mouseover', function getItemPage(evt) { | |
var via, createViaLink, xhr; | |
via = evt.target; | |
if (!(via.className === 'via' && !via.target)) { | |
return; | |
} | |
createViaLink = function createViaLink(evt) { | |
var xhr, itemPage, viaLink, viaURL, viaTitle, text; | |
xhr = evt.target; | |
if (!/^(?:200|304)$/.test(xhr.status)) { | |
via.textContent = 'loading error'; | |
return; | |
} | |
itemPage = range.createContextualFragment(xhr.responseText); | |
viaLink = itemPage.querySelector('a[data-gaq="product_via_link"]'); | |
if (!viaLink) { | |
via.parentNode.removeChild(via); | |
return; | |
} | |
viaURL = viaLink.href; | |
viaTitle = viaLink.textContent; | |
text = viaURL.length > 30 ? (viaURL.slice(0, 30) + '...') : viaURL; | |
if (viaURL === viaTitle) { | |
via.textContent = text; | |
} else { | |
via.textContent = viaTitle + ' ' + text; | |
} | |
via.href = viaURL; | |
via.target = '_blank'; | |
via.insertAdjacentHTML('BeforeBegin', '<br>'); | |
}; | |
via.textContent = '...'; | |
xhr = new XMLHttpRequest(); | |
xhr.addEventListener('load', createViaLink); | |
xhr.addEventListener('error', createViaLink); | |
xhr.open('GET', via.dataset.itemUrl); | |
// 何故かFirefox 14.0.1で動かない | |
// xhr.responseType = 'document'; | |
xhr.send(null); | |
}); | |
eachAddedNodes = function eachAddedNodes(record) { | |
each.call(record.addedNodes, addVia); | |
}; | |
recordsReader = function recordsReader(callback) { | |
return function eachRecords(records) { | |
records.forEach(callback); | |
}; | |
}; | |
MO = win.MutationObserver || win.WebKitMutationObserver; | |
new MO(recordsReader(eachAddedNodes)).observe( | |
listArea, | |
{childList: true, subtree: true} | |
); | |
}(Array.prototype.forEach, window, document)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment