OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Register a rule to show the page action whenever we see a page with 'example' | 5 // Register a rule to show the page action whenever we see a page with 'example' |
6 // in the host. Send messages after registration of the rule is complete and | 6 // in the host. Send messages after registration of the rule is complete and |
7 // when the page action is clicked. | 7 // when the page action is clicked. |
8 | 8 |
9 const kRuleId = 'rule1'; | 9 const kRuleId = 'rule1'; |
10 | 10 |
11 var canvas = document.createElement('canvas'); | |
12 var ctx = canvas.getContext('2d'); | |
13 var imageData = ctx.createImageData(19, 19); | |
jbroman
2017/05/01 20:16:40
nit: Why not just "new ImageData(19, 19)"?
Devlin
2017/05/01 20:46:47
copy-paste code. Simplified.
| |
14 | |
11 var rule = { | 15 var rule = { |
12 conditions: [ | 16 conditions: [ |
13 new chrome.declarativeContent.PageStateMatcher( | 17 new chrome.declarativeContent.PageStateMatcher( |
14 {pageUrl: {hostPrefix: 'example'}}), | 18 {pageUrl: {hostPrefix: 'example'}}), |
15 ], actions: [ | 19 ], actions: [ |
16 new chrome.declarativeContent.ShowPageAction(), | 20 new chrome.declarativeContent.ShowPageAction(), |
21 new chrome.declarativeContent.SetIcon({imageData: imageData}), | |
17 ], | 22 ], |
18 id: kRuleId, | 23 id: kRuleId, |
19 }; | 24 }; |
20 | 25 |
26 console.warn(JSON.stringify(rule)); | |
jbroman
2017/05/01 20:16:40
Was this left here from debugging?
Devlin
2017/05/01 20:46:47
Whoops, thought I caught them all. Removed.
| |
27 | |
21 chrome.pageAction.onClicked.addListener(function() { | 28 chrome.pageAction.onClicked.addListener(function() { |
22 chrome.declarativeContent.onPageChanged.removeRules([kRuleId], function() { | 29 chrome.declarativeContent.onPageChanged.removeRules([kRuleId], function() { |
23 chrome.declarativeContent.onPageChanged.getRules(function(rules) { | 30 chrome.declarativeContent.onPageChanged.getRules(function(rules) { |
24 chrome.test.assertEq(0, rules.length); | 31 chrome.test.assertEq(0, rules.length); |
25 chrome.test.sendMessage('clicked and removed'); | 32 chrome.test.sendMessage('clicked and removed'); |
26 }); | 33 }); |
27 }); | 34 }); |
28 }); | 35 }); |
29 | 36 |
30 chrome.declarativeContent.onPageChanged.addRules([rule], function() { | 37 chrome.declarativeContent.onPageChanged.addRules([rule], function() { |
31 chrome.declarativeContent.onPageChanged.getRules(function(rules) { | 38 chrome.declarativeContent.onPageChanged.getRules(function(rules) { |
32 chrome.test.assertEq(1, rules.length); | 39 chrome.test.assertEq(1, rules.length); |
33 chrome.test.assertEq(kRuleId, rules[0].id); | 40 chrome.test.assertEq(kRuleId, rules[0].id); |
34 chrome.test.sendMessage('ready'); | 41 chrome.test.sendMessage('ready'); |
35 }); | 42 }); |
36 }); | 43 }); |
44 | |
45 function didThrow(func) { | |
46 var caught = false; | |
47 try { | |
48 func(); | |
49 } catch (e) { | |
50 caught = true; | |
51 } | |
52 return caught; | |
53 } | |
54 | |
55 chrome.test.runTests([ | |
56 function validationCheck() { | |
57 // Test that type constructions are properly validated. | |
58 chrome.test.assertTrue(didThrow(function() { | |
59 var matcher = new chrome.declarativeContent.PageStateMatcher( | |
60 {pageUrl: {fake: 'bogus'}}); | |
61 })); | |
62 chrome.test.succeed(); | |
63 }, | |
64 ]); | |
OLD | NEW |