A while ago I migrated my Wikipedia Search extension to Manifest 2 and almost everything works. In my extension, the extension options page has a list of available languages. You select one from the list and it saves using localStorage. The problem is that it does not work in the latest version of Wikipedia Search. The script is based on an old Chrome API example so I'm guessing that something changed. Can anyone help?
<script type="text/javascript">
// Saves options to localStorage. function save_options() { var select = document.getElementById("language"); var language = select.children[select.selectedIndex].value; localStorage["favorite_color"] = language;
// Update status to let user know options were saved. var status = document.getElementById("status"); status.innerHTML = "Saved!"; setTimeout(function() { status.innerHTML = ""; }, 750);
}
// Restores select box state to saved value from localStorage. function restore_options() { var favorite = localStorage["favorite_color"]; if (!favorite) { return; } var select = document.getElementById("language"); for (var i = 0; i < select.children.length; i++) { var child = select.children[i]; if (child.value == favorite) { child.selected = "true"; break; } }
> wrote:
> A while ago I migrated my Wikipedia Search extension to Manifest 2 and
> almost everything works. In my extension, the extension options page has a
> list of available languages. You select one from the list and it saves
> using localStorage. The problem is that it does not work in the latest
> version of Wikipedia Search. The script is based on an old Chrome API
> example so I'm guessing that something changed. Can anyone help?
> <script type="text/javascript">
> // Saves options to localStorage.
> function save_options() {
> var select = document.getElementById("language");
> var language = select.children[select.selectedIndex].value;
> localStorage["favorite_color"] = language;
> // Update status to let user know options were saved.
> var status = document.getElementById("status");
> status.innerHTML = "Saved!";
> setTimeout(function() {
> status.innerHTML = "";
> }, 750);
> }
> // Restores select box state to saved value from localStorage.
> function restore_options() {
> var favorite = localStorage["favorite_color"];
> if (!favorite) {
> return;
> }
> var select = document.getElementById("language");
> for (var i = 0; i < select.children.length; i++) {
> var child = select.children[i];
> if (child.value == favorite) {
> child.selected = "true";
> break;
> }
> }
> }
On Fri, Aug 17, 2012 at 1:02 PM, Abraham Williams <4bra...@gmail.com> wrote:
> Move the code to a js file and use <script type="text/javascript"
> src="file.js"></script> instead.
> On Thu, Aug 16, 2012 at 2:01 PM, Corbin Davenport <
> davenportcor...@gmail.com> wrote:
>> A while ago I migrated my Wikipedia Search extension to Manifest 2 and
>> almost everything works. In my extension, the extension options page has a
>> list of available languages. You select one from the list and it saves
>> using localStorage. The problem is that it does not work in the latest
>> version of Wikipedia Search. The script is based on an old Chrome API
>> example so I'm guessing that something changed. Can anyone help?
>> <script type="text/javascript">
>> // Saves options to localStorage.
>> function save_options() {
>> var select = document.getElementById("language");
>> var language = select.children[select.selectedIndex].value;
>> localStorage["favorite_color"] = language;
>> // Update status to let user know options were saved.
>> var status = document.getElementById("status");
>> status.innerHTML = "Saved!";
>> setTimeout(function() {
>> status.innerHTML = "";
>> }, 750);
>> }
>> // Restores select box state to saved value from localStorage.
>> function restore_options() {
>> var favorite = localStorage["favorite_color"];
>> if (!favorite) {
>> return;
>> }
>> var select = document.getElementById("language");
>> for (var i = 0; i < select.children.length; i++) {
>> var child = select.children[i];
>> if (child.value == favorite) {
>> child.selected = "true";
>> break;
>> }
>> }
>> }
> --
> You received this message because you are subscribed to the Google Groups
> "Chromium Apps" group.
> To post to this group, send email to chromium-a...@chromium.org.
> To unsubscribe from this group, send email to
> chromium-apps+unsubscr...@chromium.org.
> For more options, visit this group at
> http://groups.google.com/a/chromium.org/group/chromium-apps/?hl=en.
None of those suggestions seemed to work. However I did run it in the Developer's Tools console and it said: Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:".
Since you said more code might help, here's basically the entire extension:
// Saves options to localStorage.
function save_options() {
var select = document.getElementById("language");
var language = select.children[select.selectedIndex].value;
localStorage["favorite_color"] = language;
// Update status to let user know options were saved.
var status = document.getElementById("status");
status.innerHTML = "Saved!";
setTimeout(function() {
status.innerHTML = "";
}, 750);
}
// Restores select box state to saved value from localStorage.
function restore_options() {
var favorite = localStorage["favorite_color"];
if (!favorite) {
return;
}
var select = document.getElementById("language");
for (var i = 0; i < select.children.length; i++) {
var child = select.children[i];
if (child.value == favorite) {
child.selected = "true";
break;
}
}
}
This is basically all the code in the entire extension. Any help would be very much appreciated :)
> wrote:
> None of those suggestions seemed to work. However I did run it in the
> Developer's Tools console and it said: Refused to execute inline event
> handler because it violates the following Content Security Policy
> directive: "script-src 'self' chrome-extension-resource:".
> Since you said more code might help, here's basically the entire extension:
> // Saves options to localStorage.
> function save_options() {
> var select = document.getElementById("**language");
> var language = select.children[select.**selectedIndex].value;
> localStorage["favorite_color"] = language;
> // Update status to let user know options were saved.
> var status = document.getElementById("**status");
> status.innerHTML = "Saved!";
> setTimeout(function() {
> status.innerHTML = "";
> }, 750);
> }
> // Restores select box state to saved value from localStorage.
> function restore_options() {
> var favorite = localStorage["favorite_color"]**;
> if (!favorite) {
> return;
> }
> var select = document.getElementById("**language");
> for (var i = 0; i < select.children.length; i++) {
> var child = select.children[i];
> if (child.value == favorite) {
> child.selected = "true";
> break;
> }
> }
> }
> This is basically all the code in the entire extension. Any help would be
> very much appreciated :)
so in body, where you have onload, that is not allowed anymore
also any inline script, like onclick or anything, is not allowed in v2
either
for body do....
document.body.addEventListener('load',function(){
var favorite = localStorage["favorite_color"];
if (!favorite) {
return;
}
var select = document.getElementById("language");
for (var i = 0; i < select.children.length; i++) {
var child = select.children[i];
if (child.value == favorite) {
child.selected = "true";
break;
}
}
> wrote:
> None of those suggestions seemed to work. However I did run it in the
> Developer's Tools console and it said: Refused to execute inline event
> handler because it violates the following Content Security Policy
> directive: "script-src 'self' chrome-extension-resource:".
> Since you said more code might help, here's basically the entire extension:
> // Saves options to localStorage.
> function save_options() {
> var select = document.getElementById("**language");
> var language = select.children[select.**selectedIndex].value;
> localStorage["favorite_color"] = language;
> // Update status to let user know options were saved.
> var status = document.getElementById("**status");
> status.innerHTML = "Saved!";
> setTimeout(function() {
> status.innerHTML = "";
> }, 750);
> }
> // Restores select box state to saved value from localStorage.
> function restore_options() {
> var favorite = localStorage["favorite_color"]**;
> if (!favorite) {
> return;
> }
> var select = document.getElementById("**language");
> for (var i = 0; i < select.children.length; i++) {
> var child = select.children[i];
> if (child.value == favorite) {
> child.selected = "true";
> break;
> }
> }
> }
> This is basically all the code in the entire extension. Any help would be
> very much appreciated :)
> wrote:
> None of those suggestions seemed to work. However I did run it in the
> Developer's Tools console and it said: Refused to execute inline event
> handler because it violates the following Content Security Policy
> directive: "script-src 'self' chrome-extension-resource:".
> Since you said more code might help, here's basically the entire extension:
> // Saves options to localStorage.
> function save_options() {
> var select = document.getElementById("**language");
> var language = select.children[select.**selectedIndex].value;
> localStorage["favorite_color"] = language;
> // Update status to let user know options were saved.
> var status = document.getElementById("**status");
> status.innerHTML = "Saved!";
> setTimeout(function() {
> status.innerHTML = "";
> }, 750);
> }
> // Restores select box state to saved value from localStorage.
> function restore_options() {
> var favorite = localStorage["favorite_color"]**;
> if (!favorite) {
> return;
> }
> var select = document.getElementById("**language");
> for (var i = 0; i < select.children.length; i++) {
> var child = select.children[i];
> if (child.value == favorite) {
> child.selected = "true";
> break;
> }
> }
> }
> This is basically all the code in the entire extension. Any help would be
> very much appreciated :)
First off, thanks for all your suggestions. Now when I click the Save button on the Options page it displays the 'Saved!' message. However when I search in the extension it only goes to English Wikipedia. I did the Console on the options page again and it said: Uncaught TypeError: Cannot call method 'addEventListener' of null
Here's the new language.js file, with all of the additions you guys suggested:
// Saves options to localStorage. function save_options() { var select = document.getElementById("language"); var language = select.children[select.selectedIndex].value; localStorage["favorite_color"] = language;
// Update status to let user know options were saved. var status = document.getElementById("status"); status.innerHTML = "Saved!"; setTimeout(function() { status.innerHTML = ""; }, 750);
}
// Restores select box state to saved value from localStorage. function restore_options() { var favorite = localStorage["favorite_color"]; if (!favorite) { return; } var select = document.getElementById("language"); for (var i = 0; i < select.children.length; i++) { var child = select.children[i]; if (child.value == favorite) { child.selected = "true"; break; } }
}
// Replaces onclick for the Save button window.onload = function(){ document.querySelector('input[value="Save"]').onclick=save_options;
}
// Replaces body onLoad document.body.addEventListener('load',function(){ var favorite = localStorage["favorite_color"]; if (!favorite) { return; } var select = document.getElementById("language"); for (var i = 0; i < select.children.length; i++) { var child = select.children[i]; if (child.value == favorite) { child.selected = "true"; break; } }
> First off, thanks for all your suggestions. Now when I click the Save
> button on the Options page it displays the 'Saved!' message. However when I
> search in the extension it only goes to English Wikipedia. I did the
> Console on the options page again and it said: Uncaught TypeError: Cannot
> call method 'addEventListener' of null
> Here's the new language.js file, with all of the additions you guys
> suggested:
> // Saves options to localStorage.
> function save_options() {
> var select = document.getElementById("language");
> var language = select.children[select.selectedIndex].value;
> localStorage["favorite_color"] = language;
> // Update status to let user know options were saved.
> var status = document.getElementById("status");
> status.innerHTML = "Saved!";
> setTimeout(function() {
> status.innerHTML = "";
> }, 750);
> }
> // Restores select box state to saved value from localStorage.
> function restore_options() {
> var favorite = localStorage["favorite_color"];
> if (!favorite) {
> return;
> }
> var select = document.getElementById("language");
> for (var i = 0; i < select.children.length; i++) {
> var child = select.children[i];
> if (child.value == favorite) {
> child.selected = "true";
> break;
> }
> }
> }
> // Replaces onclick for the Save button
> window.onload = function(){
> document.querySelector('input[value="Save"]').onclick=save_options;
> }
> // Replaces body onLoad
> document.body.addEventListener('load',function(){
> var favorite = localStorage["favorite_color"];
> if (!favorite) {
> return;
> }
> var select = document.getElementById("language");
> for (var i = 0; i < select.children.length; i++) {
> var child = select.children[i];
> if (child.value == favorite) {
> child.selected = "true";
> break;
> }
> }
> });