Using Google reCaptcha with forms that already have submit event handlers.

We had to jump through some hoops to get Google reCAPTCHA v3 to work with OpenCart 4.0.2.3 data-oc-toggle="ajax" form attributes. reCAPTCHA implementation is simple and straight-forward, and I really liked the ajax form processor instead of the post/response/redirect flow. I was not aware however that those new ajax forms were going to be a headache while writing a our grecaptcha module.

Other developers opt to populate the challenge response during page load, even though that can easily result in expired challenges, and is discouraged by Google. So we attempted various ways to call grecaptcha.execute() before form submission, but simply could not trigger our listener before other listeners that were added earlier in the DOM.

Continue reading

Instant NATO/Military Phonetic Alphabet Conversion With Javascript Bookmark

My spouse works remotely and regularly needs to rattle off long cryptic strings by phone. Most people don’t have a phonetic alphabets memorized and stumble when trying to convey complex strings verbally.

Within about an hour, I gave her this one-click bookmark that prompts for input, then displays the NATO phonetic equivalent.

javascript:if(input=prompt("Characters")){for(i=0,result=[],letters={a:"Alfa",b:"Bravo",c:"Charlie",d:"Delta",e:"Echo",f:"Foxtrot",g:"Golf",h:"Hotel",i:"India",j:"Juliett",k:"Kilo",l:"Lima",m:"Mike",n:"November",o:"Oscar",p:"Papa",q:"Quebec",r:"Romeo",s:"Sierra",t:"Tango",u:"Uniform",v:"Victor",w:"Whiskey",x:"Xray",y:"Yankee",z:"Zulu"};i<input.length;i++)char=input[i].toLowerCase(),item=void 0!==letters[char]?letters[char]:char,result[result.length]=input[i]==input[i].toLowerCase()?item.toLowerCase():item.toUpperCase();result.length&&(output=alert(input+":\r\n"+result.join(" ").replace(/\s+/g," ")))}
Continue reading

Reorder MySQL AUTO_INCREMENT indexes

Saved for future reference in case original source is removed. Code modified slightly, no improvements, personal tastes.

SET @row = 0;
UPDATE `table` SET `id` = @row:= @row + 1;
ALTER TABLE `table` AUTO_INCREMENT = 1;

Used this to reorder rows in a settings table, which doesn’t rely on the auto-increment primary key for uniqueness. I prefer using composite keys in such circumstances, to avoid useless indexes, but the database schema is not mine to redesign.

Fast Ways to Determine Google Analytics Client ID (client_id)

Open any page containing your gtag.js code and enter one of the following console commands…


gtag('get', window.dataLayer[1][1], 'client_id', function(result) {console.log(result)})

Note: Unmodified gtag.js code snippet presumed. If the above code fails, try replacing window.dataLayer[1][1] with 'YOUR-STREAM-MEASUREMENT-ID' or refer to methods below.


document.cookie.split(';').forEach((element) => { var split = element.split('='); if ('_ga' == split[0].trim()) console.log(split[1].substring(6))})

The Client ID can also be obtained by viewing your browser’s cookies. Look for the cookie named _ga and your client_id will the two long segments of digits including a decimal point (example from mine below).

Example: GA1.1.0000000000.0000000000


Why you might need the Google Analytics Client ID

The client_id is necessary for sending offline events or posting server-side events using Google Analytics’ Measurement Protocol.

Obtain Base URL of Current PHP Script

Includes standard SSL header check for setting scheme…

$base_url = (isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) === 'https')) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . dirname($_SERVER['SCRIPT_NAME']) . '/';

If you don’t need the header checks…

Continue reading

How to Stop All QuickBooks Processes Still Running in the Background After the Desktop Application Has Been Closed

QuickBooks resource management in Windows has always been atrocious. Even if you don’t need an app or service, Intuit loads it, and doesn’t close said ancillary applications and services after you close QuickBooks. The only acceptable reasons are sharing company files over the network, or so an app reopens faster after closing.

Continue reading

MySQL ALTER TABLE ADD COLUMN IF NOT EXISTS

I had been looking for a one-line solution for adding columns to tables when they do not already exist. For the longest time I settled on using a temporary stored procedure, but this still required at least four lines to execute using the 3rd party system I am sometimes constrained within…

DROP PROCEDURE IF EXISTS `AddColumnIfNotExists`;
DELIMITER $$ CREATE PROCEDURE `AddColumnIfNotExists`() BEGIN DECLARE CONTINUE HANDLER FOR SQLEXCEPTION BEGIN END; ALTER TABLE `t1` ADD `c1` TEXT; END $$ DELIMITER ;
CALL `AddColumnIfNotExists`();
DROP PROCEDURE `AddColumnIfNotExists`;

While that works fine, I was still displeased with having to use a stored procedure to achieve the desirable result. So, I eventually came up with this beautiful one-liner…

Continue reading

C# Ignore DropDownButtonPressed for ToolStripButton Converted to ToolStripSplitButton

When you convert a ToolStripButton to a ToolStripSplitButton, the underlying event code is not automatically converted. So you may notice clicking the split button’s down-down arrow also triggers the button’s Click event.

You can either move the Click event code to the new ButtonClick event (preferred), or adjust the Click event to ignore the drop-down arrow.

Continue reading

C# Pass Click Event to MenuStrip or ToolStrip Control Without Form Focus

More often than not, I want the click event for menus (MenuStrip) and toolbars (ToolStrip) to trigger on the first click, even when the firm does not have focus. I figure if Windows is going to show the hover effects, the user expects the click to trigger without having to first focus focus on the form.

A work around is to use your own ToolStrip and let the mouse activation give the control the focus, which in turn will then let the button fire it’s click event:

LarsTechStackOverflow Answer
Continue reading

Two ways to have your C# application run during Windows start-up

The simplest method (in my opinion) relies on the Windows registry.

using Microsoft.Win32;

namespace WindowsStartupTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            checkBoxRegistry.Checked = (registryKey().GetValue(Application.ProductName) != null);

        }
        private RegistryKey registryKey()
        {
            return Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run")!;
        }
        private void checkBoxRegistry_Click(object sender, EventArgs e)
        {
            if (checkBoxRegistry.Checked)
            {
                registryKey().SetValue(Application.ProductName, Application.ExecutablePath);
            }
            else
            {
                registryKey().DeleteValue(Application.ProductName, false);
            }
        }
    }
}

Another method is to add an application shortcut to the Windows Startup folder.

Continue reading