Bookmarklet/JavaScript to rotate URL - javascript

I want to create a Bookmarklet which will load one link out of a list of ten links, order doesn't matter, but weight-age does.
I tried http://www.htmlbasix.com/textrotator.shtml but it's for rotating a link on a webpage, how to make a Bookmarklet which will open a random URL from the list? Kind of URL rotator script within a bookmark.
Any efficient way to do that?
Thanks in advance.

You can't setup bookmarklet to run automatically. Consider writing browser extension or just use curl.
Not automatically.
Then it varies.
First, if you're 100% that pages don't redirect you anywhere you can try to use window.location inside you bookmarklet in next way:
var next = urls.indexOf(window.location.href) + 1;
next = next < urls.length ? next : 0;
window.location = urls[next];
If one of pages redirects or messes with url then you can use localStorage on your own domain and postMessage to store any data between bookmarklet calls.

I have found the solution after few tries
javascript: (function randomlinks() {
var myrandom = Math.round(Math.random() * 9);
var links = new Array();
links[0] = "http://www.javascriptkit.com";
links[1] = "http://www.dynamicdrive.com";
links[2] = "http://www.cssdrive.com";
links[3] = "http://www.codingforums.com";
links[4] = "http://www.news.com";
links[5] = "http://www.gamespot.com";
links[6] = "http://www.msnbc.com";
links[7] = "http://www.cnn.com";
links[8] = "http://news.bbc.co.uk";
links[9] = "http://www.news.com.au";
window.location = links[myrandom];
})()

Related

how to Restrict access to some urls of wordpress website from single referrer domain

hello my question is what is the best approach to Restrict access to some urls of wordpress website to single referrer domain.
as far as I am familar with javascript I found a way for that. but I think javascript code is not good, because the source code of the page does not change.
I wrote this code:
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
}
document.body.style.display="none";
var url = document.referrer;
var domainname;
var referal_code = getCookie("protect_faq_pages");
console.log(url);
if(url){
var anchor = document.createElement("a");
anchor.href = url;
domainname = anchor.host;
console.log(domainname);
if(domainname == "softwareservicetech.com"){
var cookieString = "protect_faq_pages=cWs#fgf$a1fD#FsC-)";
document.cookie = cookieString;
}
}else if(!(referal_code == "cWs#fgf$a1fD#FsC-)")){
document.getElementById("page").innerHTML="<p>Sorry you do not have permission to view the content</p>"
}
console.log(referal_code);
document.body.style.display="block";
this site can be accessed itself:
https://health-unity.com/
you can find out the page below is restriced on the view :
https://health-unity.com/help-centre/videos/
and also these pages too:
https://health-unity.com/help-centre/videos/video-number-2/
https://health-unity.com/help-centre/videos/video-number-1/
but when click on the link on below site (link to health-unity-videos):
https://softwareservicetech.com/testpage/
the archive page will be accessible after that. user can go to the pages below directly:
https://health-unity.com/help-centre/videos/video-number-2/
https://health-unity.com/help-centre/videos/video-number-1/
these were restricted before and now can be accessed by a cookie that is set.
but the problem is that page source still exist and did not changed by javascript code and user can view the page source. also I want that the cookie value should be hidden. because of these two problem I think javascript is not a good idea.
please share with me if there is way with javascript, php, or editing functions.php or .htaccess file to achieve this.
thank you for your response in advance
You can use $_SERVER['HTTP_REFERER'] in functions.php
For example:
<?php
add_action('init','check_referrer');
function check_referrer(){
if( str_contain($_SERVER['HTTP_REFERER'], 'https://example-domain.com/'){
// do somthing
}else{
// do somthing else
}
}
?>

Forward pages onto holding page then onto relevant page on new site

I have an old website that is being replaced (www.old.com) and a new website that's replacing it, and which has similar pages (www.new.com). I need all the pages within www.old.com to automatically redirect to a holding page (simple html page with the below countdown script and redirect function)... However, the challenge is that I then need the redirect to go to the relevant page on www.new.com.
code:
<script>
setInterval(function() {
var div = document.querySelector("#counter");
var count = div.textContent * 1 - 1;
div.textContent = count;
if (count <= 0) {
window.location.href="https://example.com";
}
}, 1000);
</script>
For example... www.old.com/about/ shows the holding page and countdown timer, then forwards to www.new.com/about-us/.
To redirect only single page
Redirect 301 /oldpage http://www.mysite.co.uk/newsubdir/newpage?
To redirect whole website permanently to other domain
Redirect 301 / http://www.new.com/
It's really important to implement a proper 301 redirect for any pages which change URL, so that the Google bot has an easy time understanding that the page has permanently moved location.
Google will then update it's index to point through specifically to the new page, and any inbound links and other signals will be attributed to the new page (usually allowing you to keep high rankings in Google search results provided all the content is still on the page, and that it's still properly optimised on-page (i.e. meta title tags, description, text copy on the page etc)).
NOTE :
This will not redirect email for your domain
Make a little modification to your Javascript code, such as
<script>
var old = window.location;
old = old.replace('http://www.old.com', '');
var newLocation = "https://example.com" + old;
setInterval(function() {
var div = document.querySelector("#counter");
var count = div.textContent * 1 - 1;
div.textContent = count;
if (count <= 0) {
window.location.href = newLocation ;
}
}, 1000);
</script>
This will redirect your page as you expected.

Go up one "folder" or segment in jQuery / Javascript

If I'm, for example, on this page
www.example.com/admin/bridge/boilerplate
What is the best way (Using plain javascript, or jQuery (Without loading another plugin) to go up one level, e.g.
www.example.com/admin/bridge
At the moment we are using
window.history.go(-1);
which interferes with submitted forms, etc.
This is used normally on a function like this:
$("button.cancel").bind("click", function( e ){
window.history.go(-1);
e.preventDefault();
});
Simple:
var url = window.location.href;
if (url.substr(-1) == '/') url = url.substr(0, url.length - 2);
url = url.split('/');
url.pop();
window.location = url.join('/');
var i = window.location.href.lastIndexOf("/");
window.location = window.location.href.substr(0,i)
thanks, but this edited:
$("#back a").click(function() {
var url = window.location.href;
if (url.substr(-1) == '/') url = url.substr(0, url.length - 2);
url = url.split('/');
url.pop();
window.location = url.join('/');
});
window.history is actually manipulating what is in your browser history, it is what the back/forward buttons in the browser use.
So, if the only way to get to the directory you are in is to navigate to it by digging down through the parent directories, this would work. However, that is never the only way to get to a page. Users are always able to bookmark the page.

Force links to open in a new window

In our web site we have embedded PDFs, and we would like to make sure that when the user clicks a link inside the PDF, it opens in a new tab or window. We have no control over the PDFs so we cannot do anything with the links themselves.
Is it possible to intercept the request in some way, for example with onbeforeunload, and force the new page to open in a separate window?
No sorry, there is no way of doing this.
This is by design.
If it was possible it would be a major security breach.
You can manipulate the links inside the PDF document to run a javascript to open links in a new window/tab. Heres how I did it with C# and iTextSharp
public static MemoryStream OpenLinksInNewWindow(MemoryStream mySource)
{
PdfReader myReader = new PdfReader(mySource);
int intPageCount = myReader.NumberOfPages;
PdfDictionary myPageDictionary = default(PdfDictionary);
PdfArray myLinks = default(PdfArray);
//Loop through each page
for (int i = 1; i <= intPageCount; i++)
{
//Get the current page
myPageDictionary = myReader.GetPageN(i);
//Get all of the annotations for the current page
myLinks = myPageDictionary.GetAsArray(PdfName.ANNOTS);
//Make sure we have something
if ((myLinks == null) || (myLinks.Length == 0))
continue;
//Loop through each annotation
foreach (PdfObject myLink in myLinks.ArrayList)
{
//Convert the itext-specific object as a generic PDF object
PdfDictionary myLinkDictionary = (PdfDictionary)PdfReader.GetPdfObject(myLink);
//Make sure this annotation has a link
if (!myLinkDictionary.Get(PdfName.SUBTYPE).Equals(PdfName.LINK))
continue;
//Make sure this annotation has an ACTION
if (myLinkDictionary.Get(PdfName.A) == null)
continue;
//Get the ACTION for the current annotation
PdfDictionary myLinkAction = (PdfDictionary)myLinkDictionary.Get(PdfName.A);
//Test if it is a URI action
if (myLinkAction.Get(PdfName.S).Equals(PdfName.URI))
{
//Replace the link to run a javascript function instead
myLinkAction.Remove(PdfName.F);
myLinkAction.Remove(PdfName.WIN);
myLinkAction.Put(PdfName.S, PdfName.JAVASCRIPT);
myLinkAction.Put(PdfName.JS, new PdfString(String.Format("OpenLink('{0}');", myLinkAction.Get(PdfName.URI))));
}
}
}
//Next we create a new document add import each page from the reader above
MemoryStream myMemoryStream = new MemoryStream();
using (Document myDocument = new Document())
{
using (PdfCopy myWriter = new PdfCopy(myDocument, myMemoryStream))
{
myDocument.Open();
for (int i = 1; i <= myReader.NumberOfPages; i++)
{
myWriter.AddPage(myWriter.GetImportedPage(myReader, i));
}
// Insert JavaScript function to open link
string jsText = "function OpenLink(uri) { app.launchURL(uri, true); }";
PdfAction js = PdfAction.JavaScript(jsText, myWriter);
myWriter.AddJavaScript(js);
myDocument.Close();
}
}
return new MemoryStream(myMemoryStream.GetBuffer());
}
I got most code from this answer: https://stackoverflow.com/a/8141831/596758
According to Aaron Digulla's answer, you can't actually do this without modifying the PDF reader. Sorry!
I think you can't do this without changing Acrobat Reader... I suggest ... some other PDF reader which doesn't use a "single document" UI. [Foxit Reader] uses tabs and can display several PDF documents.
Could this be of interest: JS to open pdf's in new window? I'm assuming that you can at least add JavaScript to the page. You shouldn't have to modify the PDF's themselves in order to open them in a new window, and even though you might not be able to modify the url's themselves you should be free to open those links in any fashion you want on your page. Or am I completely misunderstanding your question? :)

Javascript - write links to new tabs

Using Javascript in a firefox extension, I have opened a new tab. I am unaware of how I can write a link to www.google.com and other links (a whole list) in this tab, where the user can click a link and this page will open.
Thank you for your help
so far I had typed in :
var newTabBrowser2 = gBrowser.getBrowserForTab(gBrowser.selectedTab = gBrowser.addTab());
Unfortunately this won't work:
var newTabBrowser2 = gBrowser.getBrowserForTab(gBrowser.selectedTab = gBrowser.addTab());
newdocument=newTabBrowser2.contentDocument.documentElement.textContent;
newdocument.write("google<br>");
newdocument.write("yahoo<br>");
and I've tried this:
var newTabBrowser2 = gBrowser.getBrowserForTab(gBrowser.selectedTab = gBrowser.addTab());
newTabBrowser2.contentDocument.documentElement.innerHTML += "<a
href=\"http://www.google.com\">google";
but that only works when I use the debugger
Any idea why?
Thanks
It's not very clear from your question what you want. Maybe something like:
newwindow=window.open();
newdocument=newwindow.document;
newdocument.write("google<br>");
newdocument.write("yahoo<br>");
newdocument.close();
???
I don't believe you can use textContent to add HTML content to a document - you're possibly better off using the DOM to construct the HTML.
How about something like this (untested):
var newTabBrowser2 = gBrowser.getBrowserForTab(gBrowser.selectedTab = gBrowser.addTab());
newdocument=newTabBrowser2.contentDocument.documentElement;
var link=newdocument.createElement("a");
link.setAttribute("href", "http://www.google.com");
link.textContent="google";
newdocument.appendChild(link);
newdocument.appendChild(newdocument.createElement("br"));
link=newdocument.createElement("a");
link.setAttribute("href", "http://www.yahoo.com");
link.textContent="yahoo";
newdocument.appendChild(link);
newdocument.appendChild(newdocument.createElement("br"));
Alternatively, it may be possible to just write to the innerHtml of the document element.
This looks like the sort of thing you're looking for.
http://mesh.typepad.com/blog/2004/11/creating_a_new_.html
var myUrl = "http://mesh.typepad.com";
var tBrowser = document.getElementById("content");
var tab = tBrowser.addTab(myUrl);
This creates a new tab every time it's run - you can update the url of a pre-existing tab like this:
var uri = "http://mesh.typepad.com";
tBrowser.getBrowserForTab(tab).loadURI(uri);
Finally, you should be able to set the focus to the new tab:
tBrowser.selectedTab = tab;

Categories