Skip to content

Instantly share code, notes, and snippets.

@sterfry1988
Created November 15, 2011 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sterfry1988/1368689 to your computer and use it in GitHub Desktop.
Save sterfry1988/1368689 to your computer and use it in GitHub Desktop.
<script src="sample.js">
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
console.log("Request test");
if (request.method == "getLocalStorage") sendResponse({
data: localStorage[request.key]
});
else sendResponse({});
});
</script>
{
"name": "Directions",
"description": "Shows some of the features of the Context Menus API",
"version": "0.1",
"permissions": ["contextMenus"],
"options_page": "options.html",
"background_page": "background.html"
}
<html>
<head>
<title>
My Test Extension Options
</title>
</head>
<script type="text/javascript">
// Saves options to localStorage.
function save_options() {
var select = document.getElementById("tasaddr");
var startaddr = select.value;
localStorage.setItem("saddr", startaddr);
}
// Restores select box state to saved value from localStorage.
function restore_options() {
var address = localStorage.getItem("saddr");
var txtAddress = document.getElementById("tasaddr");
txtAddress.value = address;a
}
</script>
<body onload="restore_options()">
Your Starting address:
<textarea id="tasaddr" rows="2" cols="20">
Enter your address here
</textarea>
<br>
<button onclick="save_options()">
Save
</button>
</body>
</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.
// A generic onclick callback function.
function genericOnClick(info, tab) {
console.log("mouse clicked");
chrome.extension.sendRequest({
method: "getLocalStorage",
key: "saddr"
}, function (response) {
console.log("request made");
var sText = info.selectionText;
var daddr = sText.split(' ').join('+');
var saddr = response.data;
var homeTxt = "http://maps.google.com/maps?" + "saddr=" + saddr.split(' ').join('+') + "&daddr=";
var url = homeTxt + daddr;
chrome.tabs.create({
url: url
});
console.log("item " + info.menuItemId + " was clicked");
console.log("info: " + JSON.stringify(info));
console.log("tab: " + JSON.stringify(tab));
});
}
// Create one test item for each context type.
var contexts = ["selection"];
for (var i = 0; i < contexts.length; i++) {
var context = contexts[i];
var title = "Get Directions";
var id = chrome.contextMenus.create({
"title": title,
"contexts": [context],
"onclick": genericOnClick
});
console.log("'" + context + "' item:" + id);
}
;
@lawnsea
Copy link

lawnsea commented Nov 16, 2011

The closing brace and paren on line 13 are misplaced.

@sterfry1988
Copy link
Author

I updated my code here https://gist.github.com/1369092

@lawnsea
Copy link

lawnsea commented Nov 16, 2011 via email

@sterfry1988
Copy link
Author

Sorry for the messy code, I updated it all here and ill delete the other gist. This is my first time doing this thanks for the help

@sterfry1988
Copy link
Author

Right now none of the test messages are writing to the console i'm not sure where to go from here.

@hyperlink
Copy link

Your content script is not used anywhere... check your manifest and make sure you specify what URL pattern to run your content scripts against. Also content scripts have limited access to what your background and option pages have such as localStorage. Also look in the console of the pages your content script runs in to see the output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment