Skip to content

Commit

Permalink
Arne's examples of two approaches to showing a page action:
Browse files Browse the repository at this point in the history
* for particular URLs (chrome.tabs.onUpdated)
* for particular page content (content script)

TEST=none
BUG=34694

Review URL: http://codereview.chromium.org/561087

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@38264 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
kathyw@chromium.org committed Feb 5, 2010
1 parent 37e4c90 commit f3f0c05
Show file tree
Hide file tree
Showing 11 changed files with 106 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!DOCTYPE html>
<!--
* Copyright (c) 2010 The Chromium Authors. All rights reserved. Use of this
* source code is governed by a BSD-style license that can be found in the
* LICENSE file.
-->
<html>
<head>
<script>
// Called when a message is passed. We assume that the content script
// wants to show the page action.
function onRequest(request, sender, sendResponse) {
// Show the page action for the tab that the sender (content script)
// was on.
chrome.pageAction.show(sender.tab.id);

// Return nothing to let the connection be cleaned up.
sendResponse({});
};

// Listen for the content script to send a message to the background page.
chrome.extension.onRequest.addListener(onRequest);
</script>
</head>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (c) 2010 The Chromium Authors. All rights reserved. Use of this
* source code is governed by a BSD-style license that can be found in the
* LICENSE file.
*/
var regex = /sandwich/;

// Test the text of the body element against our regular expression.
if (regex.test(document.body.innerText)) {
// The regular expression produced a match, so notify the background page.
chrome.extension.sendRequest({}, function(response) {});
} else {
// No match was found.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name" : "Page action by content",
"version" : "1.0",
"description" : "Shows a page action for HTML pages containing the word 'sandwich'",
"background_page" : "background.html",
"page_action" :
{
"default_icon" : "sandwich-19.png",
"default_title" : "There's a 'sandwich' in this page!"
},
"content_scripts" : [
{
"matches" : [
"http://*/*",
"https://*/*"
],
"js" : ["contentscript.js"],
"run_at" : "document_idle",
"all_frames" : false
}
],
"icons" : {
"48" : "sandwich-48.png",
"128" : "sandwich-128.png"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<!--
* Copyright (c) 2010 The Chromium Authors. All rights reserved. Use of this
* source code is governed by a BSD-style license that can be found in the
* LICENSE file.
-->
<html>
<head>
<script>
// Called when the url of a tab changes.
function checkForValidUrl(tabId, changeInfo, tab) {
// If the letter 'g' is found in the tab's URL...
if (tab.url.indexOf('g') > -1) {
// ... show the page action.
chrome.pageAction.show(tabId);
}
};

// Listen for any changes to the URL of any tab.
chrome.tabs.onUpdated.addListener(checkForValidUrl);
</script>
</head>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "Page action by URL",
"version": "1.0",
"description": "Shows a page action for urls which have the letter 'g' in them.",
"background_page": "background.html",
"page_action" :
{
"default_icon" : "icon-19.png",
"default_title" : "There's a 'G' in this URL!"
},
"permissions" : [
"tabs"
],
"icons" : {
"48" : "icon-48.png",
"128" : "icon-128.png"
}
}

0 comments on commit f3f0c05

Please sign in to comment.