2014-07-08 2 views
1

(Tampermonkey) 사용자 스크립트로 일부 단어가있는 주석을 숨기고 싶습니다. 예를 들어, 나는 게시물 도메인 간 iframe의 자식 노드를 숨기려면

<div class="post-body"> 
<header> 
... 
</header> 
<div class="post-body-inner"> 
<div class="post-message-container" data-role="message-container"> 
<div class="publisher-anchor-color" data-role="message-content"> 
<div class="post-message " data-role="message" dir="auto"> 
<p>Abbas is nothing but puppet dog of Western savages and Nazis who want to enslave entire world.</p> 
</div> 
... 
</div> 

위해 다음 코드를 사용하여 페이지 http://www.cnn.com/2014/07/08/world/meast/mideast-tensions/index.html?hpt=hp_t1에 스크립트를

// ==UserScript== 
// @name  Hide CNN 
// @match  http://www.cnn.com/* 
// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js 
// @grant  GM_addStyle 
// @run-at document-end 
// ==/UserScript== 

$('div.post-body:contains("Abbas")').hide() 

을 적용하려하지만 스크립트는 아무것도하지 않는 것. 내가 도대체 ​​뭘 잘못하고있는 겁니까? 사용자 스크립트로 웹 페이지의 동적으로로드 된 부분을 필터링하는 것이 가능합니까?

UPD : 문제는 주석이 다른 도메인의 iframe에로드된다는 것입니다. Tampermonkey로 이러한 iframe의 자식 노드를 숨기려면 어떻게해야합니까? 어떻게 든 GM_xmlhttpRequest을 사용해야합니까?

+1

무엇 가능성이 일어나고있는 것은 스크립트가 실행될 때, Disqus에가 아직로드 가지고 있다는 것입니다. 당신이해야 할 일은 적합한 이벤트, 아마도'load()'이벤트 (http://api.jquery.com/load-event/)에 바인딩하는 것이라고 생각합니다. 개발자 콘솔에서 동일한 명령을 실행하려고하면 정상적으로 작동하는 것처럼 보입니다. 잘못된 시간대에 실행 중일 수 있습니다. –

+0

흠. 개발자 콘솔에서'$ ('. post-body')'를 실행하는 주석이있는 완전히로드 된 페이지에서도 주요 기사에서 노드를 선택하지만 'null'을 반환합니다. '$ ('.cnn_storypgraphtxt')', 예상대로 작동 ... – xivaxy

+0

#disqus_tread는 페이지가로드 될 때 전혀 비어 있습니다. 콘텐츠는 AJAX에 의해 동적으로 생성됩니다. MutationObserver를 사용하여 새로운 내용이 추가 된 시점을 인식해야합니다. AJAX 콘텐츠가 추가되면 window.onload가 실행되지 않습니다. –

답변

1

Disqus- 평점은 일반적으로 <iframe>에로드됩니다. 따라서 스크립트를 기본 사이트에서 실행하도록 설정하는 대신 iframe src URL에서 실행되도록 설정하십시오. 이 경우 http://disqus.com/embed/comments/...

또한이 주석은 AJAX로 구동됩니다. 따라서 AJAX savvy techniques을 사용해야합니다. 이 같은

스크립트는 (셀렉터를 조정해야 할 수도 있습니다) 작동합니다 :

// ==UserScript== 
// @name  Hide select CNN comments 
// @match http://disqus.com/embed/comments/* 
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js 
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js 
// @grant GM_addStyle 
// ==/UserScript== 
/*- The @grant directive is needed to work around a design change 
    introduced in GM 1.0. It restores the sandbox. 
*/ 

//--- Only run on comments for CNN pages 
if (/&f=cnn&/i.test (location.search)) { 
    waitForKeyElements ('li.post:contains("Abbas")', hideComment); 
} 

function hideComment (jNode) { 
    jNode.hide(); 
} 
+0

좋은 답변입니다. 어떻게하면 주 문서 URL을 식별하고 해당 Disqus 주석이있는 CNN 페이지에서만 실행되는지 확인할 수 있습니까? –