Fork me on GitHub
Current releases are 2.1.3 (9 Sept 2014) and 2.0.18.13 (5 Aug 2014)

Ready for 2.1? Find out if your favorite plugins are compatible. The 2.0 branch will no longer receive updates after Dec 2014.

How to add a javascript inside a post or thread

aeryaery Gtricks Forum in 2.2 :) ✭✭✭
edited April 2012 in Vanilla 2.0 Help

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

Pockets addon wont do that. Any other solution?

Vanilla is sweet & support is great, to encourage volunteers, please donate.

Best Answers

Answers

  • peregrineperegrine ☯ hasta luego :) In-Flight 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.

    Monetary Donations will be appreciated if you use my plugins. Thanks in Advance.
    As a waiter gets a tip for a good meal, tips for successful solutions appreciated as well. Peregrine

  • peregrineperegrine ☯ hasta luego :) In-Flight 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
    

    Monetary Donations will be appreciated if you use my plugins. Thanks in Advance.
    As a waiter gets a tip for a good meal, tips for successful solutions appreciated as well. Peregrine

  • peregrineperegrine ☯ hasta luego :) In-Flight 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.        
    

    }

    Monetary Donations will be appreciated if you use my plugins. Thanks in Advance.
    As a waiter gets a tip for a good meal, tips for successful solutions appreciated as well. Peregrine

    422
  • hbfhbf wiki guy? MVP

    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 Gtricks Forum in 2.2 :) ✭✭✭

    @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.

    Vanilla is sweet & support is great, to encourage volunteers, please donate.

  • aeryaery Gtricks Forum in 2.2 :) ✭✭✭

    @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.

    Vanilla is sweet & support is great, to encourage volunteers, please donate.

  • 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.

  • 422422 Developer MVP

    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.

    422 Real Estate Australia , now open Check it out

  • peregrineperegrine ☯ hasta luego :) In-Flight 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() {
    
        }
    
    }
    

    Monetary Donations will be appreciated if you use my plugins. Thanks in Advance.
    As a waiter gets a tip for a good meal, tips for successful solutions appreciated as well. Peregrine

  • aeryaery Gtricks Forum in 2.2 :) ✭✭✭

    @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?

    Vanilla is sweet & support is great, to encourage volunteers, please donate.

  • aeryaery Gtricks Forum in 2.2 :) ✭✭✭
    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.

    Vanilla is sweet & support is great, to encourage volunteers, please donate.

  • 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.

  • peregrineperegrine ☯ hasta luego :) In-Flight MVP

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

    Monetary Donations will be appreciated if you use my plugins. Thanks in Advance.
    As a waiter gets a tip for a good meal, tips for successful solutions appreciated as well. Peregrine

  • aeryaery Gtricks Forum in 2.2 :) ✭✭✭

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

    Vanilla is sweet & support is great, to encourage volunteers, please donate.

  • aeryaery Gtricks Forum in 2.2 :) ✭✭✭

    Unfortunately, editing body field after running

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

    didn not work.

    I couldnt find raw option.

    Vanilla is sweet & support is great, to encourage volunteers, please donate.

  • peregrineperegrine ☯ hasta luego :) In-Flight MVP
    edited April 2012

    .

    Monetary Donations will be appreciated if you use my plugins. Thanks in Advance.
    As a waiter gets a tip for a good meal, tips for successful solutions appreciated as well. Peregrine

  • aeryaery Gtricks Forum in 2.2 :) ✭✭✭

    Worked like a charm. Thanks @Underdog.

    Vanilla is sweet & support is great, to encourage volunteers, please donate.

Sign In or Register to comment.