我正在製作一個攔截廣告的程序。我找到了一個來自this頁面的廣告服務器列表。我的問題:是否有可能從使用JavaScript的網站中的該頁面搜索這些廣告服務器?並且請注意,我想要擴展代碼在javascript中搜索包含廣告服務器列表的文本文件
回答
你可以使用這樣的事情:
var sites = ['site1', 'site2', '...'],
sites_len = sites.length;
var els = document.getElementsByTagName('*');
for (var i = 0, len = els.length; i < len; i++) {
var attr = "";
switch (els[i].tagName.toLowerCase()) {
case 'iframe':
case 'script':
case 'img':
attr = 'src';
break;
case 'link':
case 'a':
attr = 'href';
break
default:
continue;
}
var attr_val = els[i].getAttribute(attr);
for (var j = 0; j < sites_len; j++)
if (sites[j].indexOf(attr_val) > -1)
els[i].parentNode.reamoveChild(els[i]);
}
默認情況下,網頁無法訪問JavaScript中另一個網頁的內容。這就是所謂的cross-origin HTTP request。該網站可以允許不同的網站使用HTTP標頭Access-Control-Allow-Origin
訪問其內容,但是您的網頁並沒有這樣做。
如果你繼續前進,嘗試你會得到錯誤XMLHttpRequest cannot load https://pgl.yoyo.org/as/serverlist.php?hostformat=nohtml&showintro=1&startdate%5Bday%5D=&startdate%5Bmonth%5D=&startdate%5Byear%5D=. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
要解決這個限制,你可以開發一個瀏覽器擴展,而不是或使用代理像crossorigin.me。
這是一個工作示例:
function loadAdServers(callback) {
const url = 'https://crossorigin.me/https://pgl.yoyo.org/as/serverlist.php?hostformat=nohtml&showintro=1&startdate%5Bday%5D=&startdate%5Bmonth%5D=&startdate%5Byear%5D=';
const request = new XMLHttpRequest();
request.open('GET', url);
request.addEventListener('load', function() {
callback(request.response.split('\n').filter(x => x));
});
request.send();
}
function removeElement(element) {
if (element.parentElement) {
element.parentElement.removeChild(element);
}
}
function removeAdElements(adServers) {
for (const img of document.querySelectorAll('img')) {
for (const adServer of adServers) {
if (img.src.indexOf(adServer) >= 0) {
removeElement(img);
}
}
}
for (const a of document.querySelectorAll('a')) {
for (const adServer of adServers) {
if (a.href.indexOf(adServer) >= 0) {
removeElement(a);
}
}
}
}
loadAdServers(removeAdElements);
<img src="http://101order.com/company.logo">
<a href="http://101order.com/">Link</a>
<a href="http://stackoverflow.com">stackoverflow</a>
這需要一些時間來加載列表和過濾元件。
謝謝你的回答。但是我試圖把它放到一個擴展中,但它不起作用。你有任何想法如何在擴展中使用它? – Vagif
我很抱歉,但我從未做過延期。我想你不需要使用'https:// crossorigin.me',但我不知道如何從擴展中訪問DOM。 – Waterscroll
- 1. 在列表中包含固定廣告
- 2. 在報告中包含文本文件
- 3. 如果源包含彈性搜索服務器中的給定搜索文本,則獲取所有文檔
- 4. 在服務器上搜索文件名
- 5. 如何使用javascript搜索服務器端文件中的特定文本?
- 6. 在服務器端包含文件中包含DIV元素
- 7. 在沒有服務器的情況下在本地文件中包含文件
- 8. 搜索包含指定文本的div
- 9. 在Windows 7文件擴展名中搜索包含'#'的文件
- 10. 使用grep來搜索文件,其中包含一些文本
- 11. 搜索含有分隔文本列表的文本「」
- 12. 如何在服務器端包含javascript文件?
- 13. 報告服務:如果正文包含
- 14. 空文件夾搜索服務器
- 15. 正則表達式:在搜索中包含文本字符串
- 16. python:在列表中搜索文本文件中的值
- 17. 在文件列表搜索
- 18. 在node.js腳本中包含JavaScript文件
- 19. Eclipse - 包含文本的文件搜索 - regex包含空白組合?
- 20. 如何搜索包含特定文本字符串的文件?
- 21. 搜索包含模式的文件
- 22. 安裝廣告(廣告)服務器
- 23. 部分linq文本搜索包含
- 24. 在Eclipse中搜索包含文本'querystring'的項目中的所有文件
- 25. 從文件列表中搜索文件
- 26. 包含和排除的文件服務器端或使用JavaScript
- 27. 將lib包含到nodejs服務器上的javascript文件
- 28. 在PHP中包含服務器端JavaScript
- 29. 在文本文件中搜索列的正則表達式
- 30. 在本地網絡服務器上搜索文件directorys
你可以對你要如何阻止這些廣告更精確?如果您打算通過刪除標籤手動禁用鏈接,圖像或iframe,我猜基本的搜索和替換頁面中的html會做.. – Kaddath
@Kaddath是的我想禁用鏈接和圖像。如果你可以提供一個很好的答案 – Vagif
在我的答案中爲你添加了一個工作示例。如果你想過濾掉更多的元素,你可以添加更多的循環。 – Waterscroll