开发者

Why doesn't this user script work in Opera? [closed]

开发者 https://www.devze.com 2023-04-07 11:25 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not generally applic
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 10 years ago.

This is my userscript:

// ==UserScript==
// @name           wykopSpamBlock
// @namespace      wykopSpamBlock
// @description    Skrypt ukrywający sponsorowane i polecane wykopy w serwisie wykop.pl 3.0
// @include        http://www.*wykop.pl/
// @include        http://www.*wykop.pl/strona/*
// @include        http://www.*wykop.pl/wykopalisko*
// @include        http://www.*wykop.pl/hity* 
// ==/UserScript==

(function() {

  function block(element) {

    element.style.display = 'none';
    console.log('It works');

  }

  //////////////////////////////////////////////////////////////////////

  var Filter = new RegExp('^http://www.wykop.pl/link/partnerredirect/');
  var Entries = document.getElementById('body-con').getElementsByClassName('entry');
  var Links;

  //////////////////////////////////////////////////////////////////////

  for (i = 0; i < Entries.length; i++) {

    Links = Entries.item(i).getElementsByTagName('div').item(0).getElementsByClassName('content').item(0).getElementsByTagName('header').item(0).getElementsByTagName('p').item(0).getElementsByTagName('a');

    if (Filter.test(Links.item(1).href) || Filter.test(Links.item(2).href)) {
      block(Entries.item(i));
    }

  }

})();

It works fine in Chrome and Firefox (GreaseMonkey), but it doesn't in Opera. I get this error:

User Javascript thread
Uncaught exception: TypeError: Cannot convert 'Entries.item(i).getElementsByTagName('div').item(0).getElementsByClassName('content').item(0)' to object
Error thrown at line 30, column 4 in <anonymous function>():
    Links = Entries.item(i).getElementsByTagName('div').item(0).getElementsByClassName('content').item(0).getElementsByTagName('header').item(0).getElementsByTagName('p').item(0).getElementsByTagName('a');
called from line 11, column 0 in program code:
    (function() {

And the question is, what's wrong?

//edit

For people who can't read that:

// ==UserScript==
// @name           wykopSpamBlock
// @namespace      wykopSpamBlock
// @description    Skrypt ukrywający sponsorowane i polecane wykopy w serwisie wykop.pl 3.0
// @include        开发者_如何学编程http://www.*wykop.pl/
// @include        http://www.*wykop.pl/strona/*
// @include        http://www.*wykop.pl/wykopalisko*
// @include        http://www.*wykop.pl/hity* 
// ==/UserScript==

(function() {

  function block(element) {

    element.style.display = 'none';
    console.log('It works');

  }

  //////////////////////////////////////////////////////////////////////

  var Filter = new RegExp('^http://www.wykop.pl/link/partnerredirect/');
  var Entries = document.getElementById('body-con').getElementsByClassName('entry');

  //////////////////////////////////////////////////////////////////////

  for (i = 0; i < Entries.length; i++) {

    var EntriesItem = Entries.item(i);
    var Div = EntriesItem.getElementsByTagName('div');
    var DivItem = Div.item(0);
    var Content = DivItem.getElementsByClassName('content');
    var ContentItem = Content.item(0);
    var Header = ContentItem.getElementsByTagName('header');
    var HeaderItem = Header.item(0);
    var Paragraph = HeaderItem.getElementsByTagName('p');
    var ParagraphItem = Paragraph.item(0);
    var Links = ParagraphItem.getElementsByTagName('a');

    if (Filter.test(Links.item(1).href) || Filter.test(Links.item(2).href)) {
      block(Entries.item(i));
    }

  }

})();

and error:

[26.09.2011 20:35:07] JavaScript - http://www.wykop.pl/
User Javascript thread
Uncaught exception: TypeError: Cannot convert 'ContentItem' to object
Error thrown at line 35, column 4 in <anonymous function>():
    var Header = ContentItem.getElementsByTagName('header');
called from line 11, column 0 in program code:
    (function() {

//edit 2

Here is full html of entry. Content starts in 21 line.

<article class="entry brbotte8 pding15_0  {id:891309}">
  <div class="clr rel">

    <div class="fleft diggbox">
      <a href="http://www.wykop.pl/link/dig/%7E2/891309/55a55552de3fac6ececb6a8933938e71-1317062985/log_ref_0,index,log_ref_m_0,index,log_ref_n_0," class="block tcenter tdnone diggit ">
        <span class="icon inlblk diggcount cff5917 large fbold vtop animated ">
          22
        </span>
        <span class="block action small br3 bre3">
          wykop
        </span>
      </a>
    </div>

    <a href="http://www.wykop.pl/ramka/891309/policjant-strzelil-nastolatkowi-w-twarz-taserem-wideo/" class="image  rel fright">
      <div class="lazy">
        <!--<img src="http://c0692282.3.cdn.imgwykop.pl/hi_34VehOZEwdnDXdwlMEBOWbkmNGIzrPM0.jpg" alt="Policjant strzelił nastolatkowi w twarz taserem [wideo" class="fright border marginleft15" width="104" height="74" />-->
      </div>
    </a>

    <div class="content">

      <header>
        <h2 class="xx-large lheight20">
          <a href="http://www.wykop.pl/ramka/891309/policjant-strzelil-nastolatkowi-w-twarz-taserem-wideo/"  class="link">
            <span class="fbold">Policjant strzelił nastolatkowi w twarz taserem [wideo</span>
          </a>
        </h2>
        <p class="small cc6">
          <a href="http://www.wykop.pl/reklama/" class="link gray" title="Przejdź do Reklama Wykop.pl">
            <span>
              Wykop Poleca
            </span>
          </a> 
          dodany 
          <time title="2011-09-26 19:37:24" datetime="2011-09-26T19:37:24+02:00" pubdate>
            1 godz. temu
          </time> 
          przez 
          <a href="http://www.wykop.pl/ludzie/Saper86/" class="link gray color color-1">
            <span>
              Saper86
            </span>
          </a>
           z 
          <a href="http://www.wykop.pl/link/partnerredirect/891309/policjant-strzelil-nastolatkowi-w-twarz-taserem-wideo/" rel="nofollow" class="link gray" title="Przejdź do Policjant strzelił nastolatkowi w twarz taserem [wideo">
            <span>
              gadzetomania.pl
            </span>
          </a>                                      
          do 
          <a href="http://ciekawostki.wykop.pl/" title="Ciekawostki" class="link gray" rel="nofollow">
            <span>
              Ciekawostki
            </span>
          </a>
          <a href="http://www.wykop.pl/link/891309/policjant-strzelil-nastolatkowi-w-twarz-taserem-wideo/" class="marginleft10 caf small tdnone">
            <span class="icon inlblk comments mini vtop margintop5 ">&nbsp;</span> 
            <span class="hvline  link gray">13 komentarzy</span>
          </a>
        </p>
      </header>

      <p class="lheight18">
        <a href="http://www.wykop.pl/link/891309/policjant-strzelil-nastolatkowi-w-twarz-taserem-wideo/" class="block ce1">
          <span class="c22">Oceńcie sami czy postąpił właściwie</span>
        </a>
      </p>

    </div>

  </div>

</article>


I'd suggest breaking each piece of this long line into individual steps and find out which of the 8 steps is failing and that should give you a clue:

Links = Entries.item(i).getElementsByTagName('div').item(0).getElementsByClassName('content').item(0).getElementsByTagName('header').item(0).getElementsByTagName('p').item(0).getElementsByTagName('a');

I really have no idea what's causing the problem. It works for both of us in jsFiddle, just not in the greasemonkey environment. Do you have any other greasemonkey scripts that are modifying the page and changing the DOM on you?

Other than that, the only other thing I can think of is to switch to using document.querySelectorAll() when it's present. Since that is supported in recent versions of Opera, that may avoid whatever issues your code is having. You can see a sample implementation in this jsFiddle: http://jsfiddle.net/jfriend00/5hJLn/. This selector doesn't generate exactly the same result as your code with any HTML, but I think it works for the HTML you have. You can consider tweaking the selector if you want. This selector is a little less brittle because it doesn't rely on the exact position of various tags as much as your code.

The general idea (implemented in that jsFiddle) is like this:

if (document.querySelectorAll) {
    var Links = document.querySelectorAll("#body-con .content header p a");
    // turn the links we found green just to show that the code executed properly
    for (var j = 0; j < Links.length; j++) {
        Links[j].className = "target2";
    }
} else {

    var Entries = document.getElementById('body-con').getElementsByClassName('entry');


    for (var i = 0; i < Entries.length; i++) {

        var EntriesItem = Entries.item(i);
        var Div = EntriesItem.getElementsByTagName('div');
        var DivItem = Div.item(0);
        var Content = DivItem.getElementsByClassName('content');
        var ContentItem = Content.item(0);
        var Header = ContentItem.getElementsByTagName('header');
        var HeaderItem = Header.item(0);
        var Paragraph = HeaderItem.getElementsByTagName('p');
        var ParagraphItem = Paragraph.item(0);
        var Links = ParagraphItem.getElementsByTagName('a');

        // turn the links we found red just to show that the code executed properly
        for (var j = 0; j < Links.length; j++) {
            Links[j].className = "target";
        }

    }
}


I used getElementsByTagName('div').item(1) instead of getElementsByClassName('content').item(0). It works. I think there must be a bug in Opera's implemetation of getElementsByClassName.

//edit

Ehh... now I get the same error for getElementsByTagName('header').item(0)..


Change the way you implement it. Try another approaching to anonymous function and use Opera dragonfly to test it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号