JS bookmarklet to open sites in Google Translate
I want to make a JavaScript bookmarklet that will open a page inside of google translate. So if I click an element or href, it will open it in google translate. It could also be disabled by clicking the bookmarklet again.
I have written something that I think should work, it would give every "a" element (not sure if it would work with other links) an event_listener which would on click stop the default execution of the link, and store the href into a variable so I can implement it into the opening of Google Translate, then later remove that event_listener(haven't done that yet).
The problem is that it alerts (which is just to test that I got the link) every single link, but it comes out as undefined.
javascript:
var A = document.getElementsByTagName('A');
var link = null
for(var i=0;i<A.length;i++){
A[i].addEventListener("click", getLink(i));
A[i].addEventListener("click", stopDirect());
}
function stopDirect() {
event.preventDefault();
}
function getLink(e) {
link = e.href;
alert (link);
}
This is the code to open a link in Google Translate
window.open("https://translate.google.com/translate?hl=en&sl=es&tl=en&u=" + thaturl+ "&anno=2");
javascript redirect hyperlink bookmarklet
add a comment |
I want to make a JavaScript bookmarklet that will open a page inside of google translate. So if I click an element or href, it will open it in google translate. It could also be disabled by clicking the bookmarklet again.
I have written something that I think should work, it would give every "a" element (not sure if it would work with other links) an event_listener which would on click stop the default execution of the link, and store the href into a variable so I can implement it into the opening of Google Translate, then later remove that event_listener(haven't done that yet).
The problem is that it alerts (which is just to test that I got the link) every single link, but it comes out as undefined.
javascript:
var A = document.getElementsByTagName('A');
var link = null
for(var i=0;i<A.length;i++){
A[i].addEventListener("click", getLink(i));
A[i].addEventListener("click", stopDirect());
}
function stopDirect() {
event.preventDefault();
}
function getLink(e) {
link = e.href;
alert (link);
}
This is the code to open a link in Google Translate
window.open("https://translate.google.com/translate?hl=en&sl=es&tl=en&u=" + thaturl+ "&anno=2");
javascript redirect hyperlink bookmarklet
add a comment |
I want to make a JavaScript bookmarklet that will open a page inside of google translate. So if I click an element or href, it will open it in google translate. It could also be disabled by clicking the bookmarklet again.
I have written something that I think should work, it would give every "a" element (not sure if it would work with other links) an event_listener which would on click stop the default execution of the link, and store the href into a variable so I can implement it into the opening of Google Translate, then later remove that event_listener(haven't done that yet).
The problem is that it alerts (which is just to test that I got the link) every single link, but it comes out as undefined.
javascript:
var A = document.getElementsByTagName('A');
var link = null
for(var i=0;i<A.length;i++){
A[i].addEventListener("click", getLink(i));
A[i].addEventListener("click", stopDirect());
}
function stopDirect() {
event.preventDefault();
}
function getLink(e) {
link = e.href;
alert (link);
}
This is the code to open a link in Google Translate
window.open("https://translate.google.com/translate?hl=en&sl=es&tl=en&u=" + thaturl+ "&anno=2");
javascript redirect hyperlink bookmarklet
I want to make a JavaScript bookmarklet that will open a page inside of google translate. So if I click an element or href, it will open it in google translate. It could also be disabled by clicking the bookmarklet again.
I have written something that I think should work, it would give every "a" element (not sure if it would work with other links) an event_listener which would on click stop the default execution of the link, and store the href into a variable so I can implement it into the opening of Google Translate, then later remove that event_listener(haven't done that yet).
The problem is that it alerts (which is just to test that I got the link) every single link, but it comes out as undefined.
javascript:
var A = document.getElementsByTagName('A');
var link = null
for(var i=0;i<A.length;i++){
A[i].addEventListener("click", getLink(i));
A[i].addEventListener("click", stopDirect());
}
function stopDirect() {
event.preventDefault();
}
function getLink(e) {
link = e.href;
alert (link);
}
This is the code to open a link in Google Translate
window.open("https://translate.google.com/translate?hl=en&sl=es&tl=en&u=" + thaturl+ "&anno=2");
javascript redirect hyperlink bookmarklet
javascript redirect hyperlink bookmarklet
edited Dec 11 '18 at 2:05
JS Lover
asked Nov 13 '18 at 4:07
JS LoverJS Lover
12
12
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Maybe you can try this one.
<a href="javascript:;" onclick="redirect('your_url_here')">Click Me!</a>
<script>
function redirect(url) {
window.open(url, "_blank")
}
</script>
You can refer here: https://www.w3schools.com/jsref/met_win_open.asp
add a comment |
So listen to the click event on the document and read the target path....
document.body.addEventListener("click", function (event) {
console.log()
if (event.target.tagName === "A" && event.shiftKey) {
console.log(event.target.href); //window.open(....)
event.preventDefault();
return false;
}
})
<a href="http://www.example.com">test 1</a>
<a href="http://www.stackoverflow.com">test 2</a>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273658%2fjs-bookmarklet-to-open-sites-in-google-translate%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Maybe you can try this one.
<a href="javascript:;" onclick="redirect('your_url_here')">Click Me!</a>
<script>
function redirect(url) {
window.open(url, "_blank")
}
</script>
You can refer here: https://www.w3schools.com/jsref/met_win_open.asp
add a comment |
Maybe you can try this one.
<a href="javascript:;" onclick="redirect('your_url_here')">Click Me!</a>
<script>
function redirect(url) {
window.open(url, "_blank")
}
</script>
You can refer here: https://www.w3schools.com/jsref/met_win_open.asp
add a comment |
Maybe you can try this one.
<a href="javascript:;" onclick="redirect('your_url_here')">Click Me!</a>
<script>
function redirect(url) {
window.open(url, "_blank")
}
</script>
You can refer here: https://www.w3schools.com/jsref/met_win_open.asp
Maybe you can try this one.
<a href="javascript:;" onclick="redirect('your_url_here')">Click Me!</a>
<script>
function redirect(url) {
window.open(url, "_blank")
}
</script>
You can refer here: https://www.w3schools.com/jsref/met_win_open.asp
answered Nov 13 '18 at 4:17
Agent DroidAgent Droid
916
916
add a comment |
add a comment |
So listen to the click event on the document and read the target path....
document.body.addEventListener("click", function (event) {
console.log()
if (event.target.tagName === "A" && event.shiftKey) {
console.log(event.target.href); //window.open(....)
event.preventDefault();
return false;
}
})
<a href="http://www.example.com">test 1</a>
<a href="http://www.stackoverflow.com">test 2</a>
add a comment |
So listen to the click event on the document and read the target path....
document.body.addEventListener("click", function (event) {
console.log()
if (event.target.tagName === "A" && event.shiftKey) {
console.log(event.target.href); //window.open(....)
event.preventDefault();
return false;
}
})
<a href="http://www.example.com">test 1</a>
<a href="http://www.stackoverflow.com">test 2</a>
add a comment |
So listen to the click event on the document and read the target path....
document.body.addEventListener("click", function (event) {
console.log()
if (event.target.tagName === "A" && event.shiftKey) {
console.log(event.target.href); //window.open(....)
event.preventDefault();
return false;
}
})
<a href="http://www.example.com">test 1</a>
<a href="http://www.stackoverflow.com">test 2</a>
So listen to the click event on the document and read the target path....
document.body.addEventListener("click", function (event) {
console.log()
if (event.target.tagName === "A" && event.shiftKey) {
console.log(event.target.href); //window.open(....)
event.preventDefault();
return false;
}
})
<a href="http://www.example.com">test 1</a>
<a href="http://www.stackoverflow.com">test 2</a>
document.body.addEventListener("click", function (event) {
console.log()
if (event.target.tagName === "A" && event.shiftKey) {
console.log(event.target.href); //window.open(....)
event.preventDefault();
return false;
}
})
<a href="http://www.example.com">test 1</a>
<a href="http://www.stackoverflow.com">test 2</a>
document.body.addEventListener("click", function (event) {
console.log()
if (event.target.tagName === "A" && event.shiftKey) {
console.log(event.target.href); //window.open(....)
event.preventDefault();
return false;
}
})
<a href="http://www.example.com">test 1</a>
<a href="http://www.stackoverflow.com">test 2</a>
answered Nov 13 '18 at 4:22
epascarelloepascarello
152k13133182
152k13133182
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273658%2fjs-bookmarklet-to-open-sites-in-google-translate%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown