Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

How to add a javascript inside a post or thread

aeryaery ✭✭✭
edited April 2012 in Vanilla 2.0 - 2.8

I would like to add adsense javascript or say any other javascript inside the content area.

Pockets addon wont do that. Any other solution?

There was an error rendering this rich post.

Best Answers

  • peregrineperegrine MVP
    edited April 2012 Answer ✓
    if it was comment id 182 ...
    
    
     $Object = ($Sender->EventArguments['Comment']);
    
    
                  if(($Object->CommentIDName) == "182") {
    

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • UnderDogUnderDog MVP
    Answer ✓

    Update GDN_Discussion SET Format='Raw' Where DiscussionID = 46

    There was an error rendering this rich post.

Answers

  • peregrineperegrine MVP
    edited April 2012

    http://vanillawiki.homebrewforums.net/index.php/Adding_Some_JQuery_to_a_Plugin

    I could probably give you a hand (if no one else chimes in) if you want to tell me what and where you want to go.

    maybe an image pointing to where you want it or your site.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • peregrineperegrine MVP
    edited April 2012

    just add $Sender->AddJsFile('plugins/Pockets/mynew.js');

    public function PluginController_Pockets_Create(&$Sender, $Args = array()) {
          $Sender->Permission('Plugins.Pockets.Manage');
          $Sender->AddSideMenu('plugin/pockets');
          $Sender->AddJsFile('plugins/Pockets/pockets.js');
          $Sender->AddJsFile('plugins/Pockets/mynew.js');
    
    
    
    or you can add it like this for a specific event.
    class JohnnyPlugin extends Gdn_Plugin {
    
     public function DiscussionsController_Render_Before(&$Sender) {
            $Sender->AddJsFile('/plugins/Johnny/js/linktopng.js');
            $Sender->AddCssFile('/plugins/Johnny/design/rainbow.css');
        }
    
    
    just throw the javascript file in /plugins/YourPlugin/js/yourjquerystuff.js
    

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • peregrineperegrine MVP
    edited April 2012

    I reread your title questions maybe something like this - no real need for jquery if you just want to reformat things on the fly. if the above doesn't work.

    public function DiscussionController_AfterCommentFormat_Handler(&$Sender) {
    
            $Object = $Sender->EventArguments['Object'];
    
            $this->DoReplacement($Object->FormatBody);
        }
    
        public function DoReplacement(&$text) {
          //  if such and such
          // wrap comment in <span class="dosomething">
          // whatever you want to do
          // possibly do a regex on something and add a class to a span that surround the body.  Then have your javasript act on the class. in the span.
           // echo the change.        
    

    }

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • If you are just trying to shove a bit of code between posts pockets will do the trick. If you need to actually perform includes i have a very much "not for distribution" plugin i cal jsInserter. I could send it to you, but you have to modify the plugin to add your code to insert. It would take me a day or two to comment it up so you could use it. PM me if its something you want.

  • aeryaery ✭✭✭

    @peregrine and @hbf thanks for answers.

    However I want some script inside the post

    like
    <script charset="utf-8" src="http://widgets.twimg.com/j/2/widget.js"></script>

    Or Adsense code inside only a specific thread, not on all the pages.

    There was an error rendering this rich post.

  • aeryaery ✭✭✭

    @peregrine and @hbf thanks for answers.

    However I want some script inside the post

    like
    <script charset="utf-8" src="http://widgets.twimg.com/j/2/widget.js"></script>

    Or Adsense code inside only a specific thread, not on all the pages.

    There was an error rendering this rich post.

  • x00x00 MVP
    edited April 2012

    why in the post. You could create a special post, but why not do it dynamically?

    you could do similar to this but for discussions:

    http://vanillaforums.org/addon/adsensebetweenthreads-plugin

    grep is your friend.

  • aery said:
    @peregrine and @hbf thanks for answers.

    However I want some script inside the post

    like
    <script charset="utf-8" src="http://widgets.twimg.com/j/2/widget.js"></script>

    Or Adsense code inside only a specific thread, not on all the pages.

    Dont insert that in a post.

    There was an error rendering this rich post.

  • peregrineperegrine MVP
    edited April 2012

    Not saying its a good idea or bad but ..

    I'm totally confused what exactly you want. But couldn't you do something like this. In this example it add the script to messages authored by mary.

    <?php if (!defined('APPLICATION'))  exit();
    
    
    // Define the plugin:
    $PluginInfo['CScript'] = array(
        'Name' => 'CScript',
        'Description' => 'Add my Script to a comment of a specific author',
        'Version' => '1.0',
        'RequiredApplications' => FALSE,
        'RequiredTheme' => FALSE,
        'RequiredPlugins' => FALSE,
        'Author' => "Peregrine"
    );
    
    class CScriptPlugin extends Gdn_Plugin {
    
    
         public function DiscussionController_AfterCommentFormat_Handler($Sender) {
    
             $Object = $Sender->EventArguments['Author'];
    
    
              if(($Object->Name) == "mary") {
    
    
    echo '
                <script type="text/javascript"><!--
                //  google_ad_client = "ca-pub-XXXXXXXXXXXXXXXX";
                //  google_ad_slot = "XXXXXXXXXX";
                //  google_ad_width = 728;
                //  google_ad_height = 90;
                //  //-->
                //  </script>
                    <script type="text/javascript"
                        src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
                    </script>';
                }
    
         }
    
    
    
        public function Setup() {
    
        }
    
    }
    

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • aeryaery ✭✭✭

    @x00 @422 I need to to this because there is a specific post that gets tons of traffic from search engine however community members are not interested in that old post. So, I thought why not monetize that page but putting some adsense units at top and bottom of writeup.

    @peregrine - Thanks for that code. Its looks that this will work for me. Can I replace "Author" in EventArguments['Author'] with post id?

    There was an error rendering this rich post.

  • aeryaery ✭✭✭
    edited April 2012

    I think adding this
    $DiscussionID = $Sender->EventArguments['Discussion']->DiscussionID;

    should do the trick. I will try this in the weekend. Please remind me if I forget.

    There was an error rendering this rich post.

  • peregrineperegrine MVP
    edited April 2012 Answer ✓
    if it was comment id 182 ...
    
    
     $Object = ($Sender->EventArguments['Comment']);
    
    
                  if(($Object->CommentIDName) == "182") {
    

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • You only want it on one post? Set the Format to Raw in the database, then enter whatever code.

    Lock the discussion.

    grep is your friend.

  • Certainly would save the extra conditional done on all comments wouldn't it.

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • aeryaery ✭✭✭

    Thats great. But the proble is, How and where do I set the format to raw?

    There was an error rendering this rich post.

  • aeryaery ✭✭✭

    Unfortunately, editing body field after running

    SELECT* FROM GDN_Discussion WHERE DiscussionID =46 LIMIT 0 , 30

    didn not work.

    I couldnt find raw option.

    There was an error rendering this rich post.

  • UnderDogUnderDog MVP
    Answer ✓

    Update GDN_Discussion SET Format='Raw' Where DiscussionID = 46

    There was an error rendering this rich post.

  • peregrineperegrine MVP
    edited April 2012

    .

    I may not provide the completed solution you might desire, but I do try to provide honest suggestions to help you solve your issue.

  • aeryaery ✭✭✭

    Worked like a charm. Thanks @Underdog.

    There was an error rendering this rich post.

Sign In or Register to comment.