Created
November 15, 2011 23:11
-
-
Save sterfry1988/1368689 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
; |
I updated my code here https://gist.github.com/1369092
Ok. What happens now? Update the SO question. You should put your code
through a formatter. It's really difficult to read.
…On Wed, Nov 16, 2011 at 1:50 PM, sterfry1988 < ***@***.*** > wrote:
I updated my code here https://gist.github.com/1369092
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1368689
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
Right now none of the test messages are writing to the console i'm not sure where to go from here.
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
The closing brace and paren on line 13 are misplaced.