protected override void OnResourceLoadComplete(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IResponse response, UrlRequestStatus status, long receivedContentLength)
        {
            if (memoryStream == null)
            {
                base.OnResourceLoadComplete(chromiumWebBrowser, browser, frame, request, response, status, receivedContentLength);
                return;
            }


            var data       = memoryStream.ToArray();
            var dataLength = data.Length;
            // For now only utf-8 enconding
            var dataAsUtf8String = Encoding.UTF8.GetString(data);

            if (!string.IsNullOrWhiteSpace(RequestResponseUTF8_script))
            {
                Logger.Log("Executing on-request-response-utf8 handler for " + request.Url, Logger.LogLevel.debug);

                var key = new Jint.Key("response");
                JSEngine.Instance.Engine.SetValue(ref key, dataAsUtf8String);
                var urlkey = new Jint.Key("url");
                JSEngine.Instance.Engine.SetValue(ref urlkey, request.Url);
                JSEngine.Instance.Execute(RequestResponseUTF8_script, ParamaterName);
            }
            base.OnResourceLoadComplete(chromiumWebBrowser, browser, frame, request, response, status, receivedContentLength);
        }
Example #2
0
        private bool executeBeforeDownloadScript(string script, string addressPattern, ref bool showDialog, DownloadItem downloadItem)
        {
            Logger.Log("Executing custom script for on-before-download handler with url: " + downloadItem.OriginalUrl, Logger.LogLevel.debug);

            var key_suggestedFilename = new Jint.Key("suggested_filename");

            JSEngine.Instance.Engine.SetValue(ref key_suggestedFilename, downloadItem.SuggestedFileName);

            var key_pageurl = new Jint.Key("page_url");

            JSEngine.Instance.Engine.SetValue(ref key_pageurl, downloadItem.OriginalUrl);

            var key_url = new Jint.Key("url");

            JSEngine.Instance.Engine.SetValue(ref key_url, downloadItem.Url);

            var key_downloadFilename = new Jint.Key("fullpath");

            JSEngine.Instance.Engine.SetValue(ref key_downloadFilename, downloadItem.FullPath);

            var key_totalBytes = new Jint.Key("total_bytes");

            JSEngine.Instance.Engine.SetValue(ref key_totalBytes, downloadItem.TotalBytes);

            var key_mimeType = new Jint.Key("mime_type");

            JSEngine.Instance.Engine.SetValue(ref key_mimeType, downloadItem.MimeType);

            var key_show_dialog = new Jint.Key("show_dialog");

            JSEngine.Instance.Engine.SetValue(ref key_show_dialog, "true");

            string result = JSEngine.Instance.ExecuteResult(script, "on-before-download<" + addressPattern + ">");

            if (result.ToString().Trim().ToLower() == "true")
            {
                return(true);
            }

            string fullpath = JSEngine.Instance.Engine.GetValue("fullpath").ToString();

            Console.WriteLine(fullpath);
            if (fullpath.ToLower().StartsWith("%desktop%"))
            {
                string pathend = fullpath.Substring("%desktop%".Length);
                fullpath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + pathend;
                Console.WriteLine(fullpath);
            }
            fullpath = fullpath.Replace("/", "\\");
            downloadItem.SuggestedFileName = fullpath;

            showDialog = JSEngine.Instance.Engine.GetValue("show_dialog").ToString().Trim().ToLower() == "true";

            return(false);
        }
Example #3
0
        /// <summary>
        /// Called to execute a command selected from the context menu. See
        /// cef_menu_id_t for the command ids that have default implementations. All
        /// user-defined command ids should be between MENU_ID_USER_FIRST and
        /// MENU_ID_USER_LAST.
        /// </summary>
        /// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
        /// <param name="browser">the browser object</param>
        /// <param name="frame">The frame the request is coming from</param>
        /// <param name="parameters">will have the same values as what was passed to</param>
        /// <param name="commandId">menu command id</param>
        /// <param name="eventFlags">event flags</param>
        /// <returns>Return true if the command was handled or false for the default implementation.</returns>
        public bool OnContextMenuCommand(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
        {
            if (_onContextScripts == null)
            {
                return(false);
            }

            int commandInt = (int)commandId;

            if (commandInt > ((int)CefMenuCommand.UserLast) || commandInt < ((int)CefMenuCommand.UserFirst))
            {
                return(false);
            }

            foreach (var rs in _onContextScripts)
            {
                if (!rs.Key.Match(frame.Url))
                {
                    continue;
                }

                string parameterName = "on-click-contextmenu";
                if (!rs.Key.MatchesEverything)
                {
                    if (rs.Key.AddressPattern != null)
                    {
                        parameterName += "<" + rs.Key.AddressPattern + ">";
                    }
                    else
                    {
                        parameterName += "<" + rs.Key.ExactAddress + ">";
                    }
                }

                setParametersInJsEngine(ref parameters, parameterName);

                commandInt -= ((int)CefMenuCommand.UserFirst);

                var key_id = new Jint.Key("clicked_id");
                JSEngine.Instance.Engine.SetValue(ref key_id, commandInt);

                Tools.Logger.Log("Executing custom script for " + parameterName + " handler", Logger.LogLevel.debug);
                JSEngine.Instance.ExecuteResult(rs.Value, parameterName);
            }

            return(true);
        }
Example #4
0
        /// <summary>
        /// Adds all necessary infos from the given parameters to the JsEngine.
        /// </summary>
        /// <param name="parameters"></param>
        /// <param name="parameterName"></param>
        private void setParametersInJsEngine(ref IContextMenuParams parameters, string parameterName)
        {
            var key_url = new Jint.Key("url");

            JSEngine.Instance.Engine.SetValue(ref key_url, parameters.PageUrl);

            var key_mediaurl = new Jint.Key("mediasource_url");

            JSEngine.Instance.Engine.SetValue(ref key_mediaurl, parameters.SourceUrl);

            var key_sourceurl = new Jint.Key("link_url");

            JSEngine.Instance.Engine.SetValue(ref key_sourceurl, parameters.LinkUrl);

            var key_mediatype = new Jint.Key("mediatype");

            JSEngine.Instance.Engine.SetValue(ref key_mediatype, Enum.GetName(typeof(ContextMenuMediaType), parameters.MediaType).ToLower());

            var key_seletedText = new Jint.Key("selected_text");

            JSEngine.Instance.Engine.SetValue(ref key_seletedText, parameters.SelectionText);

            var key_positionX = new Jint.Key("position_x");

            JSEngine.Instance.Engine.SetValue(ref key_positionX, parameters.XCoord);

            var key_positionY = new Jint.Key("position_y");

            JSEngine.Instance.Engine.SetValue(ref key_positionY, parameters.YCoord);

            var selected_types = new List <string>();
            var menutypes      = Enum.GetValues(typeof(ContextMenuType));

            string selectedTypesString = "[";

            foreach (var flagname in parameters.TypeFlags.GetFlaggedNames(true))
            {
                selectedTypesString += "\"" + flagname + "\",";
            }
            // remove tailing comma
            selectedTypesString  = selectedTypesString.Substring(0, selectedTypesString.Length - 1);
            selectedTypesString += "]";
            var key_seletedType = new Jint.Key("selected_types");

            JSEngine.Instance.Engine.SetValue(ref key_seletedType, selectedTypesString);
        }
        private string executeScript(string url, string dataString, string script)
        {
            Logger.Log("Resp. Manip.: Manipulating response of " + url + " via script of " + _parameterName, Logger.LogLevel.debug);

            var key = new Jint.Key("response");

            JSEngine.Instance.Engine.SetValue(ref key, dataString);
            var urlkey = new Jint.Key("url");

            JSEngine.Instance.Engine.SetValue(ref urlkey, url);
            string result = JSEngine.Instance.ExecuteResult(script, _parameterName);

            if (result == null || result == "undefined")
            {
                result = "";
            }
            return(result);
        }
        protected override CefReturnValue OnBeforeResourceLoad(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
        {
            if (string.IsNullOrWhiteSpace(BeforeRequest_script))
            {
                return(base.OnBeforeResourceLoad(chromiumWebBrowser, browser, frame, request, callback));
            }

            if (!string.IsNullOrWhiteSpace(ParamaterName))
            {
                Logger.Log("Executing on-before-request handler with parametername " + ParamaterName + " for url: " + request.Url, Logger.LogLevel.debug);
            }
            else
            {
                Logger.Log("Executing on-before-request handler for url: " + request.Url, Logger.LogLevel.debug);
            }


            var key_url = new Jint.Key("url");

            JSEngine.Instance.Engine.SetValue(ref key_url, request.Url);

            var key_currenturl = new Jint.Key("current_url");

            JSEngine.Instance.Engine.SetValue(ref key_currenturl, request.ReferrerUrl);

            string result = JSEngine.Instance.ExecuteResult(BeforeRequest_script, ParamaterName);

            if (!string.IsNullOrWhiteSpace(result))
            {
                if (result.ToLower() == "false")
                {
                    Logger.Log("Canceled request to: " + request.Url, Logger.LogLevel.debug);
                    return(CefReturnValue.Cancel);
                }
                if (result != "undefined")
                {
                    Logger.Log("Changed request url from " + request.Url + " to " + result, Logger.LogLevel.debug);
                    request.Url = result;
                }
            }


            return(base.OnBeforeResourceLoad(chromiumWebBrowser, browser, frame, request, callback));
        }
Example #7
0
        public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
        {
            if (!callback.IsDisposed && _rejectDownloads)
            {
                Logger.Log("Rejected download due to set reject_download to true; URL was: " + downloadItem.OriginalUrl, Logger.LogLevel.debug);
                callback.Cancel();
                return;
            }

            if (string.IsNullOrWhiteSpace(downloadItem.FullPath))
            {
                return;
            }


            foreach (var kv in _progressDownloadScripts)
            {
                if (!kv.Key.Match(downloadItem.OriginalUrl))
                {
                    continue;
                }

                Logger.Log("Executing custom script for on-progress-download handler with url: " + downloadItem.OriginalUrl, Logger.LogLevel.debug);

                string script = kv.Value;

                var key_fullpath = new Jint.Key("fullpath");
                JSEngine.Instance.Engine.SetValue(ref key_fullpath, downloadItem.FullPath);

                var key_pageurl = new Jint.Key("pageurl");
                JSEngine.Instance.Engine.SetValue(ref key_pageurl, downloadItem.OriginalUrl);

                var key_url = new Jint.Key("url");
                JSEngine.Instance.Engine.SetValue(ref key_url, downloadItem.Url);

                var key_suggestedFilename = new Jint.Key("suggested_filename");
                JSEngine.Instance.Engine.SetValue(ref key_suggestedFilename, downloadItem.SuggestedFileName);

                var key_percentage = new Jint.Key("percentage");
                JSEngine.Instance.Engine.SetValue(ref key_percentage, downloadItem.PercentComplete);

                var key_receivedBytes = new Jint.Key("received_bytes");
                JSEngine.Instance.Engine.SetValue(ref key_receivedBytes, downloadItem.ReceivedBytes);

                var key_totalBytes = new Jint.Key("total_bytes");
                JSEngine.Instance.Engine.SetValue(ref key_totalBytes, downloadItem.TotalBytes);

                var key_currentSpeed = new Jint.Key("current_speed");
                JSEngine.Instance.Engine.SetValue(ref key_currentSpeed, downloadItem.CurrentSpeed);

                var key_isComplete = new Jint.Key("is_complete");
                JSEngine.Instance.Engine.SetValue(ref key_isComplete, downloadItem.IsComplete);


                string result = JSEngine.Instance.ExecuteResult(script, "on-progress-download<" + kv.Key.AddressPattern + ">");

                if (result.ToString().Trim().ToLower() == "true")
                {
                    callback.Cancel();
                    callback.Dispose();
                    Logger.Log("Canceled download due to result of the on-progress handler; URL was: " + downloadItem.OriginalUrl, Logger.LogLevel.debug);
                    return;
                }

                break;
            }
        }
Example #8
0
        private void init_event_taskbaricon_contextmenu()
        {
            // creates the custom context menu if set
            string onbeforeContextmenu_script = Arguments.GetArgument("on-before-notifyicon-contextmenu");

            if (!string.IsNullOrWhiteSpace(onbeforeContextmenu_script))
            {
                string parameterName = "on-before-notifyicon-contextmenu";

                notifyIcon.MouseDown += (object sender, MouseEventArgs e) => {
                    if (e.Button != System.Windows.Forms.MouseButtons.Right)
                    {
                        return;
                    }

                    // Remove custom items before showing again
                    var indicesToRemove = new List <int>();
                    for (int i = 0; i < notifyIcon.ContextMenuStrip.Items.Count; i++)
                    {
                        if (notifyIcon.ContextMenuStrip.Items[i].Tag != null)
                        {
                            indicesToRemove.Add(i);
                        }
                    }
                    indicesToRemove.Reverse();
                    foreach (int index in indicesToRemove)
                    {
                        notifyIcon.ContextMenuStrip.Items.RemoveAt(index);
                    }


                    Tools.Logger.Log("Executing custom script for " + parameterName + " handler", Logger.LogLevel.debug);

                    var key_url = new Jint.Key("url");
                    JSEngine.Instance.Engine.SetValue(ref key_url, browser.GetMainFrame().Url);

                    string result_string = JSEngine.Instance.ExecuteResult(onbeforeContextmenu_script, "on-before-taskbaricon-contextmenu");
                    if (string.IsNullOrWhiteSpace(result_string) || result_string == "undefined")
                    {
                        Tools.Logger.Log("Ignored empty result from " + parameterName, Tools.Logger.LogLevel.info);
                        return;
                    }

                    Common.ApplyJsonToContextmenustrip(notifyIcon.ContextMenuStrip, result_string, "on-before-notifyicon-contextmenu");
                };
            }

            string onContextmenuClick_script = Arguments.GetArgument("on-click-notifyicon-contextmenu");

            if (!string.IsNullOrWhiteSpace(onContextmenuClick_script))
            {
                string parameterName = "on-click-notifyicon-contextmenu";

                notifyIcon.ContextMenuStrip.ItemClicked += (object sender, ToolStripItemClickedEventArgs e) => {
                    if (e.ClickedItem.Tag == null)
                    {
                        return;
                    }

                    string tag = e.ClickedItem.Tag.ToString();

                    Tools.Logger.Log("Executing custom script for " + parameterName + " handler with tag " + tag, Logger.LogLevel.debug);

                    var key_id = new Jint.Key("clicked_id");
                    JSEngine.Instance.Engine.SetValue(ref key_id, tag);

                    Tools.Logger.Log("Executing custom script for " + parameterName + " handler", Logger.LogLevel.debug);
                    JSEngine.Instance.ExecuteResult(onContextmenuClick_script, parameterName);
                };
            }
        }
Example #9
0
        private JSEngine()
        {
            this.Engine = new Jint.Engine();

            var versionKey = new Jint.Key("getVersion");

            this.Engine.SetValue(ref versionKey, new Func <object>(() => {
                return(MainController.Version);
            }));

            var logKey = new Jint.Key("log");

            this.Engine.SetValue(ref logKey, new Action <object>((obj) => {
                if (obj == null)
                {
                    return;
                }
                Logger.Log(obj.ToString(), Logger.LogLevel.info);
            }));

            var errorKey = new Jint.Key("error");

            this.Engine.SetValue(ref errorKey, new Action <object>((obj) => {
                if (obj == null)
                {
                    return;
                }
                Logger.Log(obj.ToString(), Logger.LogLevel.error);
            }));

            var writeKey = new Jint.Key("write");

            this.Engine.SetValue(ref writeKey, new Action <object>((obj) => {
                if (obj == null)
                {
                    return;
                }
                MainController.Instance.WriteOut(obj.ToString());
            }));

            var writeLineKey = new Jint.Key("writeLine");

            this.Engine.SetValue(ref writeLineKey, new Action <object>((obj) => {
                if (obj == null)
                {
                    return;
                }
                MainController.Instance.WriteOut(obj.ToString() + "\n");
            }));

            var sleepKey = new Jint.Key("sleep");

            this.Engine.SetValue(ref sleepKey, new Action <object>((obj) => {
                if (obj == null)
                {
                    return;
                }
                int ms = -1;
                if (!int.TryParse(obj.ToString(), out ms) || ms < 0)
                {
                    return;
                }
                System.Threading.Thread.Sleep(ms);
            }));


            var allCallableTypes = Common.GetAllTypes(typeof(IJintContextCallable));

            foreach (var typeKV in allCallableTypes)
            {
                var tempController = new Jint.Key(typeKV.Key);
                this.Engine.SetValue(ref tempController, typeKV.Value.GetConstructors().First().Invoke(new object[] { }));
            }


            // execute init scripts, if any
            var initialScripts = Tools.Arguments.GetStackedArguments("initial-script");

            if (initialScripts != null)
            {
                foreach (var script in initialScripts)
                {
                    string parameterName = "initial-script";
                    if (!string.IsNullOrWhiteSpace(script.Key))
                    {
                        parameterName += "<" + script.Key + ">";
                    }
                    Execute(script.Value, parameterName);
                }
            }
        }
Example #10
0
 public void SetValue(string key, string value)
 {
     Jint.Key k = new Jint.Key(key);
     Engine.SetValue(ref k, value);
 }