Giter Club home page Giter Club logo

chromoon's Introduction

Chromoon

a chrome plugin framework

View API Document on
http://seachaos.github.io/Chromoon/

View Example on
https://github.com/Seachaos/Chromoon/tree/example_get_h1/Chromoon


Example Get H1 介紹

本範例程式可以從 GitHub上取得
(https://github.com/Seachaos/Chromoon/tree/example_get_h1/Chromoon)
主要有三個部份

  • example_bg.js
  • example_pop.js
  • example_page.js

主要是示範如何取得使用者正在觀看的頁面(page)上所有h1 tag與修改內容

example_bg.js

此為背景執行的JS,在此範例中負責在Page載入頁面完成時掛載上我們的 example_page.js

	chromoon.onPageLoadScript(['example_page.js']);
	chromoon.onPageFinished(function(chromoon){
		// this code is run on font, using chromoon.setState for return data
	});

example_pop.js

此為Chrome plugin彈出對話框的JS部份
以下程式碼是在Pop準備好時註冊click事件

	chromoon.onPopReady(function(chromoon){
		$('#get_page_h1').click(function(){
			chromoon.setState({ // note1
				action : 'get_h1'
			})
		});
		$('#modify_page_h1').click(function(){
			modify_page_h1();  // note2
		});
	});

note1: 透過 setState 的方式來通知page要執行事件
note2: 執行修改page上h1的方法,內容如下

	function modify_page_h1(){
		chromoon.onPageExec(function(){
			// 此部份JS是執行在JS上的,和pop無關
			$('h1').text('Modify by Chromoon');
		});
	}

另外因為note1部份,會需要等待page回報結果,所以也有以下程式碥

	// 監聽從其他頁面 setState 來的變化
	chromoon.onStateChangeFromListener(function(chromoon, state){
		switch(state.action){ // 從 action來判定動作
			case 'found_h1':
				onfound_h1(state.h1_array); // 顯示出page回報的h1_array  
				chromoon.setState({action:'none'}); // 將 action 重置 (避免重複觸發)
				break;
		}
	});

onfound_h1 是顯示資料用

	function onfound_h1(h1_array){
		var content = "";
		for(i in h1_array){
			content += h1_array[i] + "\n";
			console.log('H1:' + h1_array[i]);
		}
		chromoon.notify(content);
	}

example_page.js

此為頁面上所執行的JS

	chromoon.onStateChangeFromListener(function(chromoon, state){
		switch(state.action){
			case 'get_h1': // 監聽送來的action, 如果是 get_h1就執行 get_page_h1();
				get_page_h1();
				break;
		}
	});

get_page_h1 部份:

	function get_page_h1(){
		var array = [];
		// 使用 JQuery來取得所有h1 tag的內文
		$('h1').each(function(){
			array.push($(this).text());
		});
		// 將事件與資料送至state, 這邊會呼叫其他的 onStateChangeFromListener
		chromoon.setState({action:'found_h1', h1_array:array});
	}

chromoon's People

Contributors

seachaos avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.