Add Quick Buttons To CocoEast/CocoWest/CocoQuebec

Speed up your workflow on the Costco deal blogs with this user script. It injects quick-copy buttons for product names and reference numbers, plus one-click search buttons for Amazon.ca, Amazon.com, and Google.

Works on:

Setup Instructions

  1. Install Tampermonkey Extension (Chrome) Tampermonkey Chrome Link After install, pin it to your toolbar.

  1. Create New Script

  • Click the Tampermonkey icon โ†’ Create a new script

  • Delete the default template code.

  • Paste the script below.

Script:

// ==UserScript==
// @name         Coco Sites: Enhanced Product Tools
// @namespace    http://tampermonkey.net/
// @version      1.4
// @description  Adds buttons to copy product name/number and search on Amazon.ca, Amazon.com, and Google
// @match        https://cocowest.ca/*
// @match        https://cocoeast.ca/*
// @match        https://cocoquebec.ca/*
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function(){
  'use strict';

  // Add CSS to the document
  const style = document.createElement('style');
  style.textContent = `
    .product-btn {
      margin-left: 4px;
      padding: 2px 6px;
      font-size: 0.8em;
      cursor: pointer;
      border: 1px solid #ccc;
      border-radius: 3px;
      background: #f8f8f8;
      transition: all 0.2s;
    }
    .product-btn:hover {
      background: #e8e8e8;
    }
    .copy-feedback {
      background: #4CAF50 !important;
      color: white !important;
    }
  `;
  document.head.appendChild(style);

  // Get the current domain
  const currentDomain = window.location.hostname;

  // The container for the post content
  const container = document.querySelector('.entry-content, #articleContent, article');
  if (!container) return;

  // Regex patterns specifically for cocoquebec.ca
  const nameRx = currentDomain === 'cocoquebec.ca'
    ? /^\s*\d+\s+(.+?)(?=\s*[~\(]|\s*\$)/  // Modified to handle French descriptions
    : /^\s*\d+\s+(.+?)(?=\s*(?:\(|\$))/;
  const skuRx = /^\s*(\d+)/;

  // Select the appropriate elements
  const selector = '.wp-caption-text';

  // Process all captions
  container
    .querySelectorAll(selector)
    .forEach(fig => {
      // Skip if we already added a button
      if (fig.dataset.copyBtnAdded) return;

      const fullText = fig.textContent.trim();

      // Extract product name
      const nameMatch = fullText.match(nameRx);
      if (!nameMatch) return;
      const productName = nameMatch[1].trim();

      // Extract product number/SKU
      const skuMatch = fullText.match(skuRx);
      const productNumber = skuMatch ? skuMatch[1].trim() : '';

      // Create button container
      const btnContainer = document.createElement('div');
      btnContainer.style.cssText = `
        margin-top: 5px;
        display: flex;
        gap: 5px;
      `;

      // 1a. Copy Name Button
      const copyNameBtn = document.createElement('button');
      copyNameBtn.innerHTML = '๐Ÿ“‹';
      copyNameBtn.title = 'Copy Product Name';
      copyNameBtn.className = 'product-btn';
      copyNameBtn.addEventListener('click', () => {
        copyToClipboard(productName, copyNameBtn);
      });

      // 1b. Copy Product Number Button
      const copyNumberBtn = document.createElement('button');
      copyNumberBtn.innerHTML = '#๏ธโƒฃ';
      copyNumberBtn.title = 'Copy Product Number';
      copyNumberBtn.className = 'product-btn';
      copyNumberBtn.addEventListener('click', () => {
        copyToClipboard(productNumber, copyNumberBtn);
      });

      // 2a. Amazon.ca search button
      const amazonCaBtn = document.createElement('button');
      amazonCaBtn.innerHTML = '๐Ÿ‡จ๐Ÿ‡ฆ';
      amazonCaBtn.title = 'Search on Amazon.ca';
      amazonCaBtn.className = 'product-btn';
      amazonCaBtn.addEventListener('click', () => {
        const searchUrl = `https://www.amazon.ca/s?k=${encodeURIComponent(productName)}`;
        window.open(searchUrl, '_blank');
      });

      // 2b. Amazon.com search button
      const amazonComBtn = document.createElement('button');
      amazonComBtn.innerHTML = '๐Ÿ‡บ๐Ÿ‡ธ';
      amazonComBtn.title = 'Search on Amazon.com';
      amazonComBtn.className = 'product-btn';
      amazonComBtn.addEventListener('click', () => {
        const searchUrl = `https://www.amazon.com/s?k=${encodeURIComponent(productName)}`;
        window.open(searchUrl, '_blank');
      });

      // 3. Google search button
      const googleBtn = document.createElement('button');
      googleBtn.innerHTML = '๐Ÿ”';
      googleBtn.title = 'Search on Google';
      googleBtn.className = 'product-btn';
      googleBtn.addEventListener('click', () => {
        const searchUrl = `https://www.google.com/search?q=${encodeURIComponent(productName)}`;
        window.open(searchUrl, '_blank');
      });

      // Add all buttons to container
      btnContainer.appendChild(copyNameBtn);
      btnContainer.appendChild(copyNumberBtn);
      btnContainer.appendChild(amazonCaBtn);
      btnContainer.appendChild(amazonComBtn);
      btnContainer.appendChild(googleBtn);

      // Insert the button container before the .ccbuttons div if it exists
      const ccButtons = fig.querySelector('.ccbuttons');
      if (ccButtons) {
        ccButtons.parentNode.insertBefore(btnContainer, ccButtons);
      } else {
        fig.appendChild(btnContainer);
      }

      fig.dataset.copyBtnAdded = 'true';
    });

  // Helper function to copy text to clipboard with feedback
  function copyToClipboard(text, button) {
    if (navigator.clipboard && navigator.clipboard.writeText) {
      navigator.clipboard.writeText(text);
    } else {
      const ta = document.createElement('textarea');
      ta.value = text;
      document.body.appendChild(ta);
      ta.select();
      document.execCommand('copy');
      ta.remove();
    }

    const originalIcon = button.innerHTML;
    button.classList.add('copy-feedback');
    button.innerHTML = 'โœ“';

    setTimeout(() => {
      button.classList.remove('copy-feedback');
      button.innerHTML = originalIcon;
    }, 1500);
  }
})()

โœ… Final Step

Click File > Save. Reload any Coco site and the buttons will auto-load under each product caption.

Last updated