Popular Posts
Install subversion with apache server on linux (ubuntu) # 更新 apt package sudo apt-get update # 安裝 package sudo apt-get install subversion apache2 libapache2-svn apache2-utils # 建立 svn 資料夾 sudo... Calculated Field Formulas Conditional formulas ... ADD ... THEN ... UNTIL ... data: begin of SERIES, N1 type I value 10, N2 type I value 20, N3 type I value 30, N4 type I value 40, ...
Stats
Reset code intelligence on eclipse ADT bundle
Window > Preference >
General > Keys
Search word completion
Click Unbind Command button
Search content assist
Change binding to Alt + /
Execute script loaded on page context

Chrome extension works on separate sandbox environment while it running. This means extension can't call script functions that loaded on page context. Due to a unexpected reason, I found a method that allow extension call functions loaded on page context from extension.

scenario

Target page
There is script function helloPage on target page. Page won't execute this function when page loaded.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script type="text/javascript">
    function helloPage() {
        for ( var i = 0; i < 10; i++) {
            var div = document.createElement('div');
            div.innerHTML = 'hello page ' + i;
            document.body.appendChild(div);
        }
    }
</script>
</head>
<body>
    <h3>Test Page</h3>
</body>
</html>
manifest.json
In manifest, extension will execute jQuery, init.js, callable.js when page is loaded, and callable.js is allowed access from web.
{
    "manifest_version": 2,
    "name": "Context test",
    "description": "Context test",
    "version": "0.3.1",
    "permissions": ["tabs", "http://*/*", "https://*/*"],
    "content_scripts": [{
        "matches": ["http://*/*", "https://*/*", "file://*/*"],
        "js": ["js/jquery-1.9.1.min.js", "js/init.js", "js/callable.js"],
        "run_at": "document_end"
    }],
    "web_accessible_resources": ["js/callable.js"]
}
init.js
This script define a function that execute a callback function like delegation. When page loaded, callable.js will be loaded by ajax call.
function dowork($callback) {
    console.log('Begin dowork at ' + new Date());
    if($callback && typeof ($callback) == 'function') $callback();
}
$(function () {
    console.log('Begin init.js at ' + new Date());
    dowork(function () {
        console.log('Dynamic load callable.js at ' + new Date());
        $.ajax({
            async: false,
            url: chrome.extension.getURL('js/callable.js'),
            dataType: 'script'
        });
    });
});
callable.js
In callback.js, it checks jQuery is loaded (should be exist in extension context) or not and print message on console. Then try to execute helloPage on page context.
(function () {
    console.log('Run callable.js at ' + new Date());
    console.log('Check jQuery status (loaded in extension context) : ' + (typeof ($) != 'undefined').toString());
    if(typeof (helloPage) == 'undefined') {
        console.log('helloPage not exist');
    } else {
        console.log('Execute page function "helloPage()"');
        helloPage();
    }
})();
Result
Afterwords

Althouth this could inject script into page context, but this may cause some security issue. And it was not the result that I expected, I hope this will be fixed in the future.

Chrome Extension: Easy Cookie

A simple cookie viewer/editor : Easy Cookie


It's easy to view cookies at a web site



Simply add a new cookie



Change cookie value if you need

Quickly parse domain name from url
(function (href) {
    var $a = document.createElement('a');
    $a.href = href;
    return $a;
})('http://nanashi07.blogspot.tw/2013/03/quickly-parse-domain-name-from-url.html').hostname
1x1 pixel transparent image
<img alt="" src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" />
HttpModule Life cycle
An HttpApplication class provides a number of events with which modules can synchronize. The following events are available for modules to synchronize with on each request. These events are listed in sequential order:
  • BeginRequest: Request has been started. If you need to do something at the beginning of a request (for example, display advertisement banners at the top of each page), synchronize this event.
  • AuthenticateRequest: If you want to plug in your own custom authentication scheme (for example, look up a user against a database to validate the password), build a module that synchronizes this event and authenticates the user how you want to.
  • AuthorizeRequest: This event is used internally to implement authorization mechanisms (for example, to store your access control lists (ACLs) in a database rather than in the file system). Although you can override this event, there are not many good reasons to do so.
  • ResolveRequestCache: This event determines if a page can be served from the Output cache. If you want to write your own caching module (for example, build a file-based cache rather than a memory cache), synchronize this event to determine whether to serve the page from the cache.
  • AcquireRequestState: Session state is retrieved from the state store. If you want to build your own state management module, synchronize this event to grab the Session state from your state store.
  • PreRequestHandlerExecute: This event occurs before the HTTP handler is executed.
  • PostRequestHandlerExecute: This event occurs after the HTTP handler is executed.
  • ReleaseRequestState: Session state is stored back in the state store. If you are building a custom session state module, you must store your state back in your state store.
  • UpdateRequestCache: This event writes output back to the Output cache. If you are building a custom cache module, you write the output back to your cache.
  • EndRequest: Request has been completed. You may want to build a debugging module that gathers information throughout the request and then writes the information to the page.
Reference : INFO: ASP.NET HTTP Modules and HTTP Handlers Overview
Loop execution of workflow

1. Create the first workflow as a trigger. This workflow will start another workflow by update current item as last action. Pause for 5 minutes is to avoid parallel execution.

2. Create the second workflow which do some action and update current item for continue loop. In this case, these two workflow will execute every day and check for status and sending mail.

Xpath in Chrome
// Query html
$x('//textarea[@id="source"]')
// Query xml
$xml = $.parseXML($('#source').val())
$x('//item[@group="true"]', $xml)
Capture response output stream using HttpModule
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace TestWebApplication
{
    public class ResponseSniffModule : IHttpModule
    {
        public void Dispose()
        {

        }

        public void Init(HttpApplication context)
        {
            context.PreRequestHandlerExecute += context_PreRequestHandlerExecute;
            context.PreSendRequestContent += context_PreSendRequestContent;
        }

        void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            var application = sender as HttpApplication;
            var context = application.Context;
            var response = context.Response;

            // Add a filter to capture response stream
            response.Filter = new ResponseSniffer(response.Filter);
        }

        void context_PreSendRequestContent(object sender, EventArgs e)
        {
            var application = sender as HttpApplication;
            var context = application.Context;
            var response = context.Response;

            var filter = response.Filter as ResponseSniffer;

            if (filter != null)
            {
                var reader = new StreamReader(new MemoryStream(filter.RecordStream.GetBuffer()));
                var source = reader.ReadToEnd();
            }
        }
    }

    public class ResponseSniffer : Stream
    {
        private readonly Stream ResponseStream;

        public MemoryStream RecordStream { get; set; }

        #region Implements of Stream
        public override bool CanRead
        {
            get { return ResponseStream.CanRead; }
        }

        public override bool CanSeek
        {
            get { return ResponseStream.CanSeek; }
        }

        public override bool CanWrite
        {
            get { return ResponseStream.CanWrite; }
        }

        public override void Flush()
        {
            ResponseStream.Flush();
        }

        public override long Length
        {
            get { return ResponseStream.Length; }
        }

        public override long Position
        {
            get
            {
                return ResponseStream.Position;
            }
            set
            {
                ResponseStream.Position = value;
            }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            return ResponseStream.Read(buffer, offset, count);
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
            ResponseStream.SetLength(value);
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            RecordStream.Write(buffer, offset, count);
            ResponseStream.Write(buffer, offset, count);
        }
        #endregion

        public ResponseSniffer(Stream stream)
        {
            RecordStream = new MemoryStream();
            ResponseStream = stream;
        }
    }
}