I am newbie as far as developing chrome extension is concerned.
I am testing an extension. In this i have a background page and options page. In options page i am storing user entered password in local storage which i want to retrieve in the background page that is on click of browser icon.
My options page is :
<html>
<head>
<title>Options for Password</title>
<script>
function savePassword()
{
window.localStorage.setItem('password', txtPassword.value);
}
</script>
</head>
<body >
<h1>Enter Password</h1>
<input type='password' name='txtPassword' id='txtPassword'/>
<br />
<button onclick='savePassword();'>Save</button>
<br />
</body>
</html>
And my background page is:
<html>
<head>
<script>
// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null, {code:"alert('password from tab:'+window.localStorage.getItem('password'));getfeedback_dialog_submitButton.setAttribute('onclick', 'if(gettext.value==\'"+window.localStorage.getItem(password)'+"\'){document.body.removeChild(getfeedbackcontainer);document.body.removeChild(getfeedbackoverlay);}');"});
});
</script>
</head>
</html>
When i save password through options page and then i click on browser action then i am getting the alert password from tab:null.
I am confused how to retrieve the value stored in the local storage?
Your code gets the localStorage value from the page where the browser action is clicked, where it is null. Change your quotes to get it at the background page:
chrome.tabs.executeScript(null, {code:"alert('password from tab:"+window.localStorage.getItem('password')+"');"});
Note that this may allow for code injection.
EDIT: Do this, it should work as long as your variables are defined properly and window.localStorage.getItem('password') is expected to be a string:
chrome.tabs.executeScript(null, {code:"alert('password from tab:"+window.localStorage.getItem('password')+"');"
+"getfeedback_dialog_submitButton.addEventListener('click', function(event){if(gettext.value=='"+window.localStorage.getItem('password')+"')"
+"{document.body.removeChild(getfeedbackcontainer);document.body.removeChild(getfeedbackoverlay);}},false);"});
I split your code into multiple lines for readability.
Related
I have a page which opens a second tab in IE11 and then in the second tab i want to be able to click a button and have that cause the first tab to load a different page. I am including a simple example which works correctly in Chrome, but not in IE11.
In the code below i explicitly set the window.name of the first tab to PascalPage, and even though i reference this name in the window.open call in page2.html it loads the Google page in the page2.html tab instead of the page1.html tab
Page 1.html
<html>
<head>
<title>Page1</title>
<script>
this.window.name="PascalPage";
function doStuff() {
window.open("page2.html", "_blank");
}
</script>
<head>
<body>
<button onclick="javascript:doStuff();" value="button">Button</button>
</body>
</html>
Page2.html
<html>
<head>
<title>Page2</title>
<script>
function doStuff() {
window.open("http://www.google.com", "PascalPage");
}
</script>
<head>
<body>
<button onclick="javascript:doStuff();" value="button">Button</button>
</body>
In my Internet Settings options the selected option is to allow IE to decide how to handle tabs.
Can anyone help?
I found the answer...
function loadUrlInTab(targetwindowname, url) {
//open the url in the targetwindowname or new tab if the target window isnt already open
winref = window.open('', targetwindowname);
winref.location.href = url;
winref.focus();
}
The window.open() call returns a reference to the window with 'targetwindowname' and then its a simple case of setting the location for that window.
PS. I found that in Chrome the instruction to focus() works, but in IE11 it doesn't. But that is a minor issue overall.
My page contains this:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="refresh" content="5; URL=http://www.example.com">
</head>
<body>
test
</body>
</html>
It redirects in Chrome, but not in Firefox. Why not?
In Firefox autorefresh has been disabled by default.
To enable autorefresh in your browser:
type about:config in the location bar of your webbrowser
a message appears: click to accept
search for blockautorefresh
change accessibility.blockautorefresh from false to true
It would be best to use alternatives such as a JavaScript or PHP Redirect.
JavaScript
window.setTimeout(function() {
window.location.href = 'http://www.google.com';
}, 5000);
PHP
header("refresh:5;url=wherever.php");
On Firefox the auto refresh is disabled by default.
You can configure Firefox manually by entering "about:config" in the browser's address bar. A warning message will appear; click on "I'll be careful, I promise!" to be able to continue.
Next, type "Accessibility.blockautorefresh" in the search box at the top of the page. Double-click the "true" value next to this preference to set it to "false" and allow the browser pages to auto-refresh.
Or use Javascript to redirect to the page.
window.setTimeout(function() {
window.location.href = "https://www.google.com/";
}, 2000);
Or you can add one line code to the body tag:
<body onload="setTimeout(location.href='https://www.google.com/', 2000)">
I want to have two pages that link to each other but stay open unless the user closes them. I know I can say target="first" and target="second" but is there any way of getting the first page to have the target name of "first" without clicking on a link to open it?
Usage:
Open page "first" by typing the address (or getting link from search engine, etc.)
Click on a link to second page - page "second" opens in a new tab
Click on a link in the second page - the link opens in the "first" tab
At the moment I end up with two copies of page "first":
Name: none, Content: first page
Name: "second", Content: second page
Name: "first", Content: first page
I want to reuse page 1 instead of opening page 3.
Is this possible? How? Can this be done with any number of pages (i.e. not relying on parent to work)?
You can use this javascript function
window.open(url,"mypopup");
Fiddle : http://jsfiddle.net/DfpG5/
Refer this answer :
How to detect if user has already opened an url and redirect to same if already opened?
The following code does what I want:
target.html:
<!DOCTYPE html>
<html>
<head>
<title>Target</title>
<script type="text/javascript">
if (document.parentWindow.name == "") {
document.parentWindow.name = "foobar";
}
</script>
</head>
<body>
Second page
</body>
</html>
target2.html:
<!DOCTYPE html>
<html>
<head>
<title>Target 2</title>
<script type="text/javascript">
if (document.parentWindow.name == "") {
document.parentWindow.name = "barfoo";
}
</script>
</head>
<body>
First page
</body>
</html>
I haven't tested it in many browsers yet so I don't know how compatible it is.
I am wondering if anyone out there would be willing to tell me how I could use the html form text box I have on my apache server to open a web address entered into my text box on my html object below it. I have a problem with iframes on my server so i'd rather use an object. Any help would be wonderful!
Thanks Carl.
You can use the following:
Whenever you have text in your input field, have a button to submit and call the function that will load html data into your div that I called includeContent for the purpose of this example.
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(function() {
$("#includeContent").load("b.html");
});
</script>
</head>
<body>
<div id="includeContent"></div>
</body>
</html>
Browser : IE6/IE7.
I want to load a new document into my html modal dialog, either via a form target or a javascript function.
<html> <!-- quirks mode -->
<head>
<script>
function openModal(url) {
if(window.showModalDialog) showModalDialog(url);
else {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite");
open(url, "", "modal=yes");
} catch (e) {
alert("dialog windows unsupported by browser");
}
}
}
</script>
</head>
<body style="background:red" onload="setTimeout(function(){document.body.style.backgroundColor='white'},100)">
Open Modal
<form>
<input type="submit" value="Send Form" />
</form>
Reload content
</body>
</html>
In Gecko-based browsers, it works.
In IE, when in the modal dialog window, the form opens a new window (even if I specify the target="_self" attribute), and the javascript reload() fails silently. If I try to do a location.replace(location.href) or location.href=_someurl_, it opens a new window.
So my question : how can I get IE to replace the current document in a modal dialog window?
window.name = "SomeWindowName";
window.open("www.microsoft.com", "SomeWindowName");
...works. this must be used within the dialog window instead of location.replace
A solution that handles the <form> part of the problem : add
<base target="_self" />
in the <head> section of the page.
It doesn't resolve the javascript issue, though.
window.name="SomeWindowName";
window.open("www.microsoft.com","SomeWindowName");
I just run into the same problem with Internet Explorer 8, so after playing around with it for a while it seems that there are some solutions such as
location.reload();return false;
or
window.location = window.location;
or
window.location.reload(true);
However, none of them work for me - either nothing happens, or, best case, a new window opens.
What I've come up with as some kind of workaround is to use
document.forms[0].submit();
It may not work for all constellations as you need a form on your page (and make sure that the form submission does not perform any unwanted changes), but it worked for me. Also make sure that you don't forget to put the
<base target="_self" />
into the header of your page as described by Alsciende.
HTH
G.