I would just like to say I am new to bookmarklets so any help would be greatly appreciated. Since most users I have come across have trouble understanding how to add a bookmarklet and use it I wanted to make a easy to use button that, when clicked, opens a new window/tab while uses the bookmarklet as it's URL.
The bookmarklet that I have requires two clicks, one click to open the page and a second click to inject the script into the head tag. I believe I should be using myWindow=window.open but after spending a couple hours trying to set it up I seem to be at a stand still. the code I used is below.
<button onclick="javascript:(function(){var a=document.createElement('script');a.type='text/javascript';a.src='https://www.weebly.com/uploads/5/0/1/8/5018607/redirect.js';try{document.getElementsByTagName('head')[0].appendChild(a);}catch(err){window.location = 'https://gamessl.ageofchampions.com/aoc/?cmd[0]=308&raid_difficulty=0';}finally {var directloc = 'https://gamessl.ageofchampions.com/aoc/?cmd[0]=308&raid_difficulty=0';if(window.location != directloc){window.location = directloc;var b=document.createElement('script');b.type='text/javascript';b.src='https://www.weebly.com/uploads/5/0/1/8/5018607/raid-collector.js';document.getElementsByTagName('head')[0].appendChild(b);}}})();">GD's Raid Collector</button>
The above code is functional but opens the bookmarklet in the same window. No matter where in this code I place myWindow=window.open it stops the bookmarklet from working all together. If I am suppose to use myWindow=window.open where would I need to put it in my coding? If not what else should I use to accomplish this task.
NOTE: One of the many ways I have tried to get this to work is as follows.
<a href="javascript:(function(){ myWindow=window.open(‘javascript:(function(){var a=document.createElement('script');a.type='text/javascript';a.src='https://www.weebly.com/uploads/5/0/1/8/5018607/redirect.js';try{document.getElementsByTagName('head')[0].appendChild(a);}catch(err){window.location = 'https://gamessl.ageofchampions.com/aoc/?cmd[0]=308&raid_difficulty=0';}finally {var directloc = 'https://gamessl.ageofchampions.com/aoc/?cmd[0]=308&raid_difficulty=0';if(window.location != directloc){window.location = directloc;var b=document.createElement('script');b.type='text/javascript';b.src='https://www.weebly.com/uploads/5/0/1/8/5018607/raid-collector.js';document.getElementsByTagName('head')[0].appendChild(b);}}})();’); })();" rel="nofollow"><button>Test Raid Collector</button></a>
Update:
Ok, so I managed to get the bookmarklet to open in a new window via the following code....
<script>
function open_win()
{ myWindow=window.open("javascript:(function(){var a=document.createElement('script');a.type='text/javascript';a.src='https://www.weebly.com/uploads/5/0/1/8/5018607/redirect.js';try{document.getElementsByTagName('head')[0].appendChild(a);}catch(err){window.location = 'https://gamessl.ageofchampions.com/aoc/?cmd[0]=308&raid_difficulty=0';}finally {var directloc = 'https://gamessl.ageofchampions.com/aoc/?cmd[0]=308&raid_difficulty=0';if(window.location != directloc){window.location = directloc;var b=document.createElement('script');b.type='text/javascript';b.src='https://www.weebly.com/uploads/5/0/1/8/5018607/raid-collector.js';document.getElementsByTagName('head')[0].appendChild(b);}}})();","GD's Raid Collector","width=800px,height=800px"); }
</script>
In connection with.....
<input type="button" value=" Test GD's Raid Collector " onclick="open_win()">
The issue now is that when I click the button a second time it does not reuse the bookmarklet like I need it to so I need the bookmarklet to use a onload function, or something, to append the script elements in the header the first time it is clicked insteasd of requiring a second click.
Again I am new to bookmarklets so if anyone can help me with this it would be greatly appreciate, thanks in advance to anyone who might be able to help me resolve this issue.
Update: Ok I recently found that setTimeout should do the trick but I need it to open the same bookmarklet after the setTimeout and I am not sure how to do this. If anyone could help it would be greatly appreciated.