inputボックスにURL打ち込むと自動URL短縮化されるjs

inputボックスにURL打ち込むと自動URL短縮化されるjs

[f:id:kenzo0107:20160310115924p:plain]

概要

最近では URL に日本語を利用するケースが増えてきました。

SEO として価値がある作業かどうかは眉唾ではありますが
ネット利用率の低い方にとっては UI としては分かりやすいのかもしれません。

その点を論じているブログがありました。

日本語を含む URL を Twitter へ投稿するようなソーシャル連携ツール等の開発の際、
文字数に厳密に注意する必要があり、短縮の自動化を bitly API で行いましたので
そのまとめです。

手順

  • 以下 js を html 等でロードしてください。

以上

By grunt, uglify js & minify css on MacOSX.

By grunt, uglify js & minify css on MacOSX.

environment

  • MacOSX 10.11
  • grunt-cli v0.1.13
  • grunt v0.4.5

Install npm, Initilize npm

1
2
3
cd ~              # depending on your preference.
brew install npm
npm init
  • Result
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
About to write to /Users/kenzo/go/src/github.com/flag/public/package.json:

{
"name": "public",
"version": "1.0.0",
"description": "gruntfile",
"main": "Gruntfile.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "xxxxxxxx@gmail.com",
"license": "ISC",
"devDependencies": {
},
"dependencies": {
}
}

Install grunt-cli

1
npm install -g grunt-cli

With --save-dev option, add install module infomation to package.json.

Install grunt modules

1
2
3
4
5
6
7
8
9
10
11
12
npm install -g grunt --save-dev
npm install grunt-contrib-watch --save-dev
npm install grunt-contrib-copy --save-dev
npm install grunt-contrib-clean --save-dev
npm install grunt-contrib-cssmin --save-dev
npm install grunt-contrib-uglify --save-dev
npm install grunt-image --save-dev
npm install grunt-contrib-htmlmin --save-dev
npm install load-grunt-tasks --save-dev
npm install grunt-jsbeautifier --save-dev
npm install grunt-cssbeautifier --save-dev

Module map.

Module Detail
grunt-contrib-watch Monitoring update files.
grunt-contrib-copy Copy file or directory.
grunt-contrib-clean Clean file or directory.
grunt-contrib-cssmin Minify CSS files.
grunt-contrib-uglify Uglify & Compress Javascript files.
grunt-contrib-image Optimize image files (jpeg, jpg, gif, png, swf, etc…).
grunt-contrib-htmlmin Minify HTML files.
grunt-jsbeautifier beautify Javascript files.
grunt-cssbeautifier beautify Javascript files.

Confirm package.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
cat package.json

{
"name": "public",
"version": "1.0.0",
"description": "grunt",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "*********@gmail.com",
"license": "ISC",
"devDependencies": {
"grunt": "^0.4.5",
"grunt-contrib-clean": "^0.6.0",
"grunt-contrib-copy": "^0.8.2",
"grunt-contrib-cssmin": "^0.14.0",
"grunt-contrib-htmlmin": "^0.6.0",
"grunt-contrib-uglify": "^0.10.0",
"grunt-contrib-watch": "^0.6.1",
"grunt-cssbeautifier": "^0.1.2",
"grunt-image": "^1.1.1",
"grunt-jsbeautifier": "^0.2.10",
"load-grunt-tasks": "^3.3.0"
}
}
  • Add dependencies of grunt modules to package.json !

move to parent directory of css, js folder

1
cd /path/to/project/public/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
tree

/public/

├─Gruntfile.coffee # make `Gruntfile.coffee` at next step.

├── css
│ ├── bootstrap.css
│ ├── img.css
│ ├── style.css
│ └── reset.css

└── js
├── jquery-1.9.1.js
├── img.js
├── login.js
└── signup.js

Create Gruntfile.coffee or Gruntfile.json

  • Today, I create a Gruntfile.coffee.
  • For example, Only Uglify js, Minify css, Beautify js, css.
1
vim Gruntfile.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module.exports = (grunt) ->

# current path.
path = require('path')
current = path.resolve('.')

# load npm task.
grunt.loadNpmTasks('grunt-contrib-cssmin');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-jsbeautifier');
grunt.loadNpmTasks('grunt-cssbeautifier');

grunt.initConfig

# define directory.
dir:
js: 'js'
css: 'css'
img: 'img'

# minify CSS.
cssmin:
all:
expand: true
cwd: current + '/<%= dir.css %>/'
src: '*.css'
dest: current + '/<%= dir.css %>/'

# uglify js.
uglify:
options:
mangle: true
compress: true
all:
expand: true
cwd: current + '/<%= dir.js %>/'
src: '*.js'
dest: current + '/<%= dir.js %>/'

# beautify js.
jsbeautifier:
files: '**/*.js'
options: []

# beautify css.
cssbeautifier:
files: '**/*.css'
options: []

grunt.registerTask 'default', ['cssmin', 'uglify']

Make symbolilc Link of ‘node_modules’

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
cd <path where Gruntfile.coffee exist>
ln -s ~/node_modules .
tree

/public/
├─Gruntfile.coffee
├─node_modules -> /Users/kenzo/node_modules

├── css
│ ├── bootstrap.css
│ ├── img.css
│ ├── style.css
│ └── reset.css

└── js
├── jquery-1.9.1.js
├── img.js
├── login.js
└── signup.js

Execute grunt command.

1
2
cd <path where Gruntfile.coffee exist>
grunt # By no parameter, execute default task.
  • If You want to execute only cssmin, excute command grunt cssmin

Thakns.