Last active
December 23, 2015 17:29
-
-
Save naosim/6669465 to your computer and use it in GitHub Desktop.
GooglePlayで、ヒットしたタイトルを削除するサンプル
ブラウザでGooglePlayを表示して、コンソールでプログラムを実行してみてください。
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
// Rubyっぽく書ける拡張 | |
var each = function(func) { | |
for(var i = 0; i < this.length; i++) { | |
func(this[i]); | |
} | |
}; | |
Array.prototype.each = each; | |
NodeList.prototype.each = each; | |
String.prototype.contains = function(word) { | |
return this.indexOf(word) != -1; | |
}; | |
// getElementsByClassNameを拡張して、callbackを受けれるようにする | |
HTMLDocument.prototype._getElementsByClassName = HTMLDocument.prototype.getElementsByClassName; | |
HTMLDocument.prototype.getElementsByClassName = function(className, callback) { | |
if(!callback) { | |
return document._getElementsByClassName(className); | |
} | |
var elms = document._getElementsByClassName(className); | |
elms.each(function(elm) { | |
callback(elm); | |
}); | |
return elms; | |
}; | |
// 要素の削除 | |
HTMLElement.prototype.deleteFromParent = function() { | |
this.parentNode.removeChild(this); | |
}; | |
// ループをまわす | |
var loop = function(interval, loopFunc) { | |
loopFunc(); | |
setTimeout(function() { | |
loop(interval, loopFunc); | |
},interval); | |
}; | |
// アプリタイトルの要素からアプリのボックス要素を取得する | |
function getBoxElmFromTitleElm(titleElm) { | |
var result = titleElm.parentNode.parentNode.parentNode; | |
return titleElm.parentNode.parentNode.parentNode; | |
} | |
// サブタイトル(会社名)からアプリのボックス要素を取得する | |
function getBoxElmFromSubTitleElm(subTitleElm) { | |
return subTitleElm.parentNode.parentNode.parentNode.parentNode; | |
} | |
var deleteElms = function(elms) { | |
elms.each(function(elm) { | |
try { | |
elm.deleteFromParent(); | |
}catch(e){} | |
}); | |
} | |
// タイトルにヒットする要素を削除する | |
function deleteByTitles(titles, className, getBoxElmFunction) { | |
var titles = (titles instanceof Array) ? titles : new Array(titles); | |
// 削除する要素を探す | |
var foundElms = []; | |
document.getElementsByClassName(className, function(titleElm) { | |
titles.each(function(title) { | |
if(titleElm.innerText.contains(title)) { | |
var boxElm = getBoxElmFunction(titleElm); | |
foundElms.push(boxElm); | |
} | |
}); | |
}); | |
// 削除 | |
deleteElms(foundElms); | |
} | |
var deleteTitles = ['Hな', 'パズドラ', 'パチスロ', 'ぱちんこ', 'パチンコ', 'パチ', '萌']; | |
var deleteSubTitles = ['LINE', 'CyberAgent', 'mobage', 'GREE', 'GMO', 'COLOPL', 'Android48', 'アリスマ', 'marge', 'MASAYA777', 'makot46', 'Vitalify', 'Goodia']; | |
loop(5000, function() { | |
// 実行 | |
deleteByTitles(deleteTitles, 'title', getBoxElmFromTitleElm); | |
deleteByTitles(deleteSubTitles, 'subtitle', getBoxElmFromSubTitleElm); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment