Pergunta

I would like to add google ads after the first paragraph on every page on my wiki. I have a code that checks if a tag is there, if it isn't then the tag is added and this tag calls the code for the google ads. However, the rest of the text on the page is not showing after the ads. This is my code: (based on the AdsWhereever extension)

  $wgHooks['ParserFirstCallInit'][] = 'AdsSetup'; $wgHooks['EditPage::showEditForm:initial'][] = 'CheckHasTag';
  function AdsSetup( &$parser ) {
     $parser->setHook( 'ads', 'AdsRender' );
      return true;}

    function AdsRender($input, $args ) {    
        $input ="";  $url = array(); global $wgOut;

        $ad['goo1'] = '<html><br><script type="text/javascript">
        google_ad_client = "xxx";
        google_ad_width = 728;
        google_ad_height = 90;
        </script>
        <script type="text/javascript"
        src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
        </script><br></html>';
   $media = $args['media'];
   return $ad[$media];
        }


         // check if content has <goo1> tag
          function CheckHasTag($editPage){global $wgOut;
           $wgOut->addScript('<script type="text/javascript">
           var editTextboxText = document.getElementById("wpTextbox1").value;
                var searchFor = "goo1";
                           var searchResult = editTextboxText.search(searchFor);
                if(searchResult == -1){
                var a = editTextboxText;
                var b = "\n<ads media=goo1>\n";
                var findP = "\n"; 
                var p = editTextboxText.search(findP);
                var position = p;
    document.getElementById("wpTextbox1").value = a.substr(0, position) + b + a.substr(position);
       }</script>');
       return true;
       }
Foi útil?

Solução 2

It was missing a / in the tag which was why the rest of the text wasn't being displayed! var b = "\n\n"

Outras dicas

Your code relies on $wgRawHtml being set to true in LocalSettings.php. (This, by the way, is a huge security risk, and should never be used in public wikis.) If you did not set $wgRawHtml to true, the last output of your tag function will be </html>, that will cause the browser to stop parsing the page, and produce no further output.

Something like this would probably work, without having to use the html-tag:

function AdsRender( $input, $args, $parser, $frame ) {

    $output = '<div id="googlead"><script type="text/javascript">
    google_ad_client = "xxx";
    google_ad_width = 728;
    google_ad_height = 90;
    </script>
    <script type="text/javascript"
    src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
    </script></div>';
    return array( $output, "markerType" => 'nowiki' );
}

Btw, you have a lot of garbage in you example, it's easier to help you if you remove code that is not being used (like $url = array(); global $wgOut; etc)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top