Skip to content

Commit aac1c06

Browse files
author
fangj
committed
bridge for html
1 parent 1011f6f commit aac1c06

32 files changed

+92
-26
lines changed

Chrome/Example/simple_extension/bridgeServerChrome.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,14 @@
1010
//bridge
1111
var bridge = {};
1212
bridge.tabIds={};
13-
bridge.send=function(msg,responseCallback){
13+
bridge.ssend=function(msg,responseCallback){
1414
assert(!responseCallback || responseCallback instanceof Function,"responseCallback should be function");
1515
//broadcast msg
16-
/*
17-
chrome.windows.getAll({"populate":true}, function (windows){
18-
for (var i = 0; i < windows.length; i++) {
19-
tabs=windows[i].tabs;
20-
for (var j = 0; j < tabs.length; j++) {
21-
22-
if(!tabs[j].url.match(/^http.+/)){
23-
continue;
24-
}
25-
26-
var tabId=tabs[j].id;
27-
chrome.tabs.sendMessage(tabId, msg, responseCallback);
28-
};
29-
};
30-
});
31-
*/
3216
for(var tabId in bridge.tabIds){
3317
chrome.tabs.sendMessage(bridge.tabIds[tabId], msg, responseCallback);
3418
}
3519
}
36-
bridge.init=function(onMessageCallback){
20+
bridge.sinit=function(onMessageCallback){
3721
var adapter=onMessageCallbackAdapter(onMessageCallback);
3822
chrome.extension.onMessage.addListener(adapter);
3923
}

Chrome/Example/simple_extension/user_client.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
console.log("user_client.js called!");
2-
3-
4-
document.addEventListener('WebViewJavascriptBridgeReady', function onBridgeReady(event) {
2+
document.addEventListener('WebViewJavascriptBridgeReady', function onBridgeReady(event) {
53
var bridge = event.bridge
64
bridge.init(function(message, responseCallback) {
75
console.log('Received message: ' + message)
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
console.log("user_server.js called!");
2-
bridge.init(function(data,responseCallback){
2+
bridge.sinit(function(data,responseCallback){
33
console.log("Received message from javascript: "+data);
44
responseCallback("Right back atcha");
55
})
66

7-
bridge.send("Well hello there");
8-
bridge.send("Give me a response, will you?", function(responseData) {
9-
console.log("Background got its response! "+responseData);
10-
})
7+
function serverSend(){
8+
bridge.ssend("Well hello there");
9+
bridge.ssend("Give me a response, will you?", function(responseData) {
10+
console.log("Background got its response! "+responseData);
11+
})
12+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//client
2+
(function(bridge){
3+
//common
4+
function assert(condition, message) {
5+
if (!condition) {
6+
throw message || "Assertion failed";
7+
}
8+
}
9+
10+
bridge.init=function(register){
11+
//assert register is a function
12+
assert(register instanceof Function,"init() should has a function as parameter");
13+
bridge.subscribe("MESSAGE2C",function(message,responseCallback){
14+
register(message,responseCallback);
15+
})
16+
}
17+
bridge.send=function(message,responseCallback){
18+
bridge.publish("MESSAGE2S",[message,responseCallback]);
19+
}
20+
//dispatch event
21+
var event=new CustomEvent('WebViewJavascriptBridgeReady')
22+
event.bridge=bridge;
23+
document.dispatchEvent(event);
24+
})(bridge)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//server
2+
bridge={};
3+
(function(d){var e=d.c_||{};d.publish=function(a,b){for(var c=e[a],f=c?c.length:0;f--;)c[f].apply(d,b||[])};d.subscribe=function(a,b){e[a]||(e[a]=[]);e[a].push(b);return[a,b]};d.unsubscribe=function(a){for(var b=e[a[0]],a=a[1],c=b?b.length:0;c--;)b[c]===a&&b.splice(c,1)}})(bridge);
4+
bridge.sinit=function(register){
5+
//assert register is a function
6+
bridge.subscribe("MESSAGE2S",function(message,responseCallback){
7+
register(message,responseCallback);
8+
})
9+
}
10+
bridge.ssend=function(message,responseCallback){
11+
bridge.publish("MESSAGE2C",[message,responseCallback]);
12+
}

Html/Example/multi_js/example.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>WebBridge Example</title>
6+
7+
<script src="bridgeServerHtml.js"></script> <!--muse be first -->
8+
<script src="user_client.js"></script>
9+
<script src="user_server.js"></script>
10+
<script src="bridgeClientHtml.js"></script> <!--muse be last -->
11+
12+
</head>
13+
<body>
14+
15+
</body>
16+
</html>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
console.log("user_client.js called!");
2+
document.addEventListener('WebViewJavascriptBridgeReady', function onBridgeReady(event) {
3+
var bridge = event.bridge
4+
bridge.init(function(message, responseCallback) {
5+
console.log('Received message: ' + message)
6+
if(responseCallback){
7+
responseCallback("Right back atcha")
8+
}
9+
})
10+
bridge.send('Hello from the javascript')
11+
bridge.send('Please respond to this', function responseCallback(responseData) {
12+
console.log("Javascript got its response", responseData)
13+
})
14+
}, false)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
console.log("user_server.js called!");
2+
bridge.sinit(function(data,responseCallback){
3+
console.log("Received message from javascript: "+data);
4+
if(responseCallback){
5+
responseCallback("Right back atcha");
6+
}
7+
})
8+
9+
function serverSend(){
10+
bridge.ssend("Well hello there");
11+
bridge.ssend("Give me a response, will you?", function(responseData) {
12+
console.log("Background got its response! "+responseData);
13+
})
14+
}
15+
16+
setTimeout(serverSend,1000);
File renamed without changes.

0 commit comments

Comments
 (0)