Processing Ajax...

Title

Message

Confirm

Confirm

Confirm

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure you want to delete this item?

Open clipboard as URL or search non-URL on Chrome

Description
This script will open Chrome and load the URL that's on the clipboard, or search for the text if it's not a URL.
Language
C#.net
Minimum Version
Created By
Wakewalker
Contributors
-
Date Created
Jan 3, 2023
Date Last Modified
Jan 3, 2023

Scripted Function (Macro) Code

// Rewritten from VB to C# on 12/10/2022

using System;
using System.Diagnostics;
using Microsoft.VisualBasic;
using System.Net;


public class ClipboardFusionHelper
{
    public static string ProcessText(string text)
    {
        string url = text;
        if (!DoesURLExists(text))
        {
            url = "http://www.google.com/search?q={0}";
            url = string.Format(url, text.Replace(' ', '+'));
        }
        Process.Start($@"""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"" --profile-directory=Default --new-tab ""{url}"" " );
        return text;
    }


    public static bool UrlIsValid(string url)
    {
        bool is_valid = false;
        if (url.ToLower().StartsWith("www."))
            url = "http://" + url;

        HttpWebResponse web_response = null;
        try
        {
            WebRequest web_request = HttpWebRequest.Create(url);
            web_response = (HttpWebResponse)web_request.GetResponse();
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
        finally
        {
            if (!(web_response == null))
                web_response.Close();
        }
    }



    public static bool URLExists(string url)
    {
        try
        {
            object Request;
            int ff;
            dynamic  rc;
			bool URLExists = false;
            Request = Interaction.CreateObject(url);

            {
                dynamic  withBlock = Request;
                withBlock.Open("GET", url, false);
                withBlock.Send();
                rc = withBlock.StatusText;
            }
            Request = null;
            if (rc == "OK")
                URLExists = true;

            
            return URLExists;
        }
        catch (Exception ex)
        {
            return false;
        }
    }



    public static bool DoesURLExists(string url)
    {
        bool responsea = false;
        try
        {
            System.Net.WebRequest webRequest = System.Net.WebRequest.Create(url);
            webRequest.Method = "HEAD";
            System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)webRequest.GetResponse();
            if ((response.StatusCode.ToString() == "OK"))
                responsea = true;
        }
        catch (Exception ex)
        {
            return responsea;
        }
        return responsea;
    }
}