Aug 092016
 

So there are some plugins out there to hide and show WordPress comments (notably the one named “Hide Show Comment“), but there are some things I didn’t like about it.

  1. They want you to pay for more options (I’m quite frugal).
  2. There is no option to hide the comment form – only the comments.

So here’s how you do it. We’ll be using jQuery (JavaScript) and inserting the code with a plugin.

  1. Install a plugin that lets you insert custom code.
    1. SEO Ultimate can do this under the “Code Inserter” section (use the “Before Item Content section).
    2. Insert Headers and Footers can do this (use the “Scripts in Header” section)
  2. Insert the script below.
<script type="text/javascript">
    jQuery(function(){
            // make the "leave a comment" text hide/show the comment form.
            jQuery('.comment-reply-title').click(function(){
                jQuery('.comment-form').slideToggle('fast');
            });

            // hide the comment form by default
            jQuery('.comment-form').hide();

            // make the text look like a link so the user knows it's clickable.
            // return false means the page will not reload.
            jQuery('.comment-reply-title').wrap("<a href="" onclick="return false;" >");

            // the reply button should always show the comment form
            jQuery('.comment-reply-link').wrap("<a href="" onclick="jQuery('.comment-form').show();" >");

            // the cancel reply link should always hide the comment form
            jQuery('.cancel-comment-reply-link').wrap("<a href="" onclick="jQuery('.comment-form').hide();" >");
        });
</script>

That’s all you need really. The rest is just nitpicking. One issue is that the comment form is not hidden by default. If we simply add some CSS to fix it, then people who have JavaScript turned off will never see the comment form. So we want to hide the comment form by default only when JavaScript is supported.

    1. Add the following JavaScript to the header.
<script type="text/javascript">
document.documentElement.className = "js";
</script>
  1. Add the following custom CSS using a plugin or your theme (for example the plugin “Simple Custom CSS“).
/** hide the comment form only if JavaScript is enabled. */
.js .comment-form { display: none; }

The JavaScript-dependent CSS was taken from https://css-tricks.com/snippets/javascript/css-for-when-javascript-is-enabled/.

Jan 222014
 
/* Copyright (c) 2014 Aaron Johnson
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 */

    /** Returns the next available non-existent file by incrementing a numerical value at the end of the file name. <br/> Copyright (c) 2014 Aaron Johnson <br/>
     * abcdefg.html -> abcdefg.1.html -> abcdefg.2.html -> abcdefg.3.html -> abcdefg.4.html <br/>
     * abcdefg -> abcdefg.1 -> abcdefg.2 -> abcdefg.3 -> abcdefg.4 <br/>
     * @param originalFile
     * @return
     */

    public static File findNextAvailableFilename(final File originalFile) {
        final Path originalPath = Paths.get(originalFile.getAbsolutePath());
        String extension = FilenameUtils.getExtension(originalPath.toString());

        // if there is no extension then we just add .
        if (extension.length() > 0) {
            extension = "." + extension;
            // if the number is in the extension then there is no extension - there is only the number.
            if (extension.matches("[.]\\d+")) {
                extension = "";
            }
        }
        Path path = originalPath;

        // loop until the file does not exist.
        while (Files.exists(path)) {
            // find and extract the number currently used.
            int number = 0;
            String newFilename = originalPath.getFileName().toString();
            if (newFilename.matches(".+[.]\\d+" + Pattern.quote(extension))) {
                number = Integer.valueOf(newFilename.replaceFirst(".+[.](\\d+)" + Pattern.quote(extension), "$1"));
            }
            number++;

            // if there is a number currently used then replace it.
            if (newFilename.matches(".+[.]\\d+" + Pattern.quote(extension))) {
                newFilename = newFilename.replaceFirst("(.+[.])(\\d+)" + Pattern.quote(extension), "$1" + number + extension);
            } else {
                // there is no number currently used so insert a number.
                final String filenameWithoutExtension = newFilename.substring(0, newFilename.length() - extension.length());
                newFilename = filenameWithoutExtension + "." + number + extension;
            }
            path = originalPath.getParent().resolve(newFilename);
        }

        return path.toFile();
    }


// -----------------------

    /** Copyright (c) 2014 Aaron Johnson <br/>
     * Test method for {@link #findNextAvailableFilename(java.io.File)}.
     * @throws IOException
     */

    @Test(timeout=3000)
    public void testFindNextAvailableFilenameWithExtension() throws IOException {
        final List<Path> filesToDelete = new LinkedList<>();
        try {
            final Path tempFolder = Paths.get(System.getProperty("java.io.tmpdir"));
            Path tempFile = tempFolder.resolve(FileDescriptionTestCase.class.getSimpleName() + ".html");

            // assure the file does not exist.
            Files.deleteIfExists(tempFile);

            // this should return the same name because the file name is available.
            File newFilename = FileUtils.findNextAvailableFilename(tempFile.toFile());
            assertEquals("The same file should have been returned because the file doesn't exist.",
                    newFilename.getAbsolutePath(), tempFile.toAbsolutePath().toString());

            // create the file.
            new FileOutputStream(tempFile.toFile()).close();
            filesToDelete.add(tempFile);
            assertTrue("The file should exist now. (This just assures that we have write access to the disk.)", Files.exists(tempFile));

            for (int i = 1; i < 100; i++) {
                newFilename = FileUtils.findNextAvailableFilename(tempFile.toFile());
                tempFile = Paths.get(newFilename.toString());
                assertFalse("File should not exist.", Files.exists(tempFile));
                assertEquals("The file was not named correctly.",
                        tempFile.getFileName().toString(), FileDescriptionTestCase.class.getSimpleName() + "." + i + ".html");

                new FileOutputStream(tempFile.toFile()).close();
                filesToDelete.add(tempFile);
                assertTrue("The file should exist now. (This just assures that we have write access to the disk.)", Files.exists(tempFile));
            }

        } finally {
            // delete all temp files
            for (final Path fileToDelete: filesToDelete) {
                try {
                    Files.deleteIfExists(fileToDelete);
                } catch (final Exception e) {
                    // ignored.
                }
            }
        }
    }

    /** Copyright (c) 2014 Aaron Johnson <br/>
     * Test method for {@link #findNextAvailableFilename(java.io.File)}.
     * @throws IOException
     */

    @Test//(timeout=3000)
    public void testFindNextAvailableFilenameWithoutExtension() throws IOException {
        final List<Path> filesToDelete = new LinkedList<>();
        try {
            final Path tempFolder = Paths.get(System.getProperty("java.io.tmpdir"));
            Path tempFile = tempFolder.resolve(FileDescriptionTestCase.class.getSimpleName());

            // assure the file does not exist.
            Files.deleteIfExists(tempFile);

            // this should return the same name because the file name is available.
            File newFilename = FileUtils.findNextAvailableFilename(tempFile.toFile());
            assertEquals("The same file should have been returned because the file doesn't exist.",
                    newFilename.getAbsolutePath(), tempFile.toAbsolutePath().toString());

            // create the file.
            new FileOutputStream(tempFile.toFile()).close();
            filesToDelete.add(tempFile);
            assertTrue("The file should exist now. (This just assures that we have write access to the disk.)", Files.exists(tempFile));

            for (int i = 1; i < 100; i++) {
                newFilename = FileUtils.findNextAvailableFilename(tempFile.toFile());
                tempFile = Paths.get(newFilename.toString());
                assertFalse("File should not exist.", Files.exists(tempFile));
                assertEquals("The file was not named correctly.",
                        tempFile.getFileName().toString(), FileDescriptionTestCase.class.getSimpleName() + "." + i);

                new FileOutputStream(tempFile.toFile()).close();
                filesToDelete.add(tempFile);
                assertTrue("The file should exist now. (This just assures that we have write access to the disk.)", Files.exists(tempFile));
            }

        } finally {
            // delete all temp files
            for (final Path fileToDelete: filesToDelete) {
                try {
                    Files.deleteIfExists(fileToDelete);
                } catch (final Exception e) {
                    // ignored.
                }
            }
        }
    }
 Posted by at 9:51 pm  Tagged with:
Mar 212012
 

Problem

For some ridiculous reason, many (or perhaps all) shows on hulu.com cannot be watched when outside of the United States. This is the message you get when attempting to watch a video while in France.

We’re sorry, currently our video library can only be streamed within the United States. For more information on Hulu’s international availability, click here.

If you’re inside the United States and believe you’ve received this message in error, please click here.

This also applies to the following problems, which conveniently gives me the opportunity to include some keywords directly in my post.

  • How to use pbskids.org from outside of the United States.
  • How to listen to pandora.com outside of the United States.
VPN (Oversimplified)

VPN (Oversimplified)

Solution

There are simple solutions though. The simplest is to visit http://hotspotshield.com/, download the free version, install and enjoy.

Should Hotspot shield ever stop functioning or should hulu block access via hotspot shield, there are a number of other free Virtual Private Network (VPN) services to choose from (see http://www.makeuseof.com/tag/7-completely-free-vpn-services-protect-privacy/).

Why It Works

The basic idea is that whenever you visit a website while using a VPN, the VPN acts as a go-between for you and the website. The website thinks it’s dealing with the VPN when in reality the VPN is passing all of the information on to you. Generally this is not the main purpose of VPNs – they are used for security. See http://en.wikipedia.org/wiki/Vpn.

Nov 252011
 

Pinterest is an image based link sharing website that’s become fairly popular lately. It has a “Pin It” button that can normally be dragged to the bookmark bar and added as a link in your browser to make it easy to “pin” things. Of course, you can’t drag a link on the iPad! There is an iPad App for Pinterest, and from what I’ve heard it leaves something to be desired. So here’s how to get a Pin It button on the iPad, iPad2, or anything else that supports running JavaScript in a bookmark but doesn’t allow you to drag and drop a link.

The easy way: add the Pin it button to your bookmark bar and sync the bookmarks between your iPad and Safari on your computer using iCloud. Thanks to Karl for this.

The longer way: The “Pin It” button is just a link with some JavaScript in it, so we will create a custom bookmark on the iPad with the correct JavaScript and it will work as expected.

  • Find the custom JavaScript.
  1. You can try copying and pasting the JavaScript from here, but it may change in the future. If it doesn’t work, then continue on to the next step in this section. Do include the “javascript:” at the beginning – it’s absolutely necessary.
    1. javascript:void((function(){var%20e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','http://assets.pinterest.com/js/pinmarklet.js?r='+Math.random()*99999999);document.body.appendChild(e)})());
  2. Open Safari on an actual computer (you may need to install it on Windows). Or just install one of those User Agent Switchers and tell Pinterest that you’re using Safari, if you know what you’re doing…
  3. Log in to Pinterest and get the “Pin It” button from the “About” menu, and drag it to your bookmarks.
  4. Edit the bookmark and copy the address of the bookmark, which should start with “javascript:”
  • Get the bookmark text on to your iPad. Probably the easiest way to do this is by emailing it somewhere and setting up that email on the iPad.
  • Copy the entire text of the bookmark, including the “javascript:”
  • Create a bookmark on the iPad (for anything, it is changed in the next step).
  • Edit the bookmark you created, name it “Pin It!”, paste the javascript:… text into the location/address part of the bookmark.
  • Click the “Pin It!” bookmark on any page with an image to get the popup window where you choose which photo to use. This look slightly different on the iPad but has the same functionality.
  • Rejoice in Pinterest goodness.