JS bookmarklet to open sites in Google Translate










0















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");









share|improve this question




























    0















    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");









    share|improve this question


























      0












      0








      0








      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");









      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 11 '18 at 2:05







      JS Lover

















      asked Nov 13 '18 at 4:07









      JS LoverJS Lover

      12




      12






















          2 Answers
          2






          active

          oldest

          votes


















          0














          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






          share|improve this answer






























            0














            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>








            share|improve this answer






















              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
              );



              );













              draft saved

              draft discarded


















              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









              0














              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






              share|improve this answer



























                0














                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






                share|improve this answer

























                  0












                  0








                  0







                  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






                  share|improve this answer













                  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







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 4:17









                  Agent DroidAgent Droid

                  916




                  916























                      0














                      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>








                      share|improve this answer



























                        0














                        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>








                        share|improve this answer

























                          0












                          0








                          0







                          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>








                          share|improve this answer













                          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>






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 13 '18 at 4:22









                          epascarelloepascarello

                          151k13131179




                          151k13131179



























                              draft saved

                              draft discarded
















































                              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.




                              draft saved


                              draft discarded














                              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





















































                              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







                              這個網誌中的熱門文章

                              How to read a connectionString WITH PROVIDER in .NET Core?

                              Node.js Script on GitHub Pages or Amazon S3

                              Museum of Modern and Contemporary Art of Trento and Rovereto