Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit 6b151c6

Browse files
committed
generic listener added
1 parent bff6486 commit 6b151c6

1 file changed

Lines changed: 38 additions & 63 deletions

File tree

javascripts/content_scripts/privly.js

Lines changed: 38 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,36 @@ var privly = {
559559
}
560560
},
561561

562+
563+
checkIframePostedMessage: function(message){
564+
565+
"use strict";
566+
567+
//check the format of the message
568+
if (typeof(message.origin) !== "string"){
569+
return;
570+
}
571+
572+
var msg = JSON.parse(message.data);
573+
574+
if(msg.command === 'resize'){
575+
privly.resizeIframePostedMessage(message);
576+
}
577+
else if(msg.command === 'hide'){
578+
privly.hideIframePostedMessage(message);
579+
}
580+
else if(msg.command === 'show'){
581+
privly.showIframePostedMessage(message);
582+
}
583+
else if(msg.command === 'remove'){
584+
privly.removeIframePostedMessage(message);
585+
}
586+
else{
587+
return;
588+
}
589+
590+
},
591+
562592
/**
563593
* Receive an iframe resize message sent by the iframe using postMessage.
564594
* Injected iframe elements need to know the height of the iframe's contents.
@@ -575,18 +605,8 @@ var privly = {
575605

576606
"use strict";
577607

578-
//check the format of the message
579-
if (typeof(message.origin) !== "string"){
580-
return;
581-
}
582-
583608
var msg = JSON.parse(message.data);
584-
585-
if(msg.command != 'resize'){
586-
return;
587-
}
588-
589-
609+
590610
//Get the element by name.
591611
var frameName = msg.frameID;
592612
var heightNew = msg.heightNew;
@@ -632,17 +652,8 @@ var privly = {
632652

633653
"use strict";
634654

635-
//check the format of the message
636-
if (typeof(message.origin) !== "string"){
637-
return;
638-
}
639-
640655
var msg = JSON.parse(message.data);
641656

642-
if(msg.command != 'hide'){
643-
return;
644-
}
645-
646657
//Get the element by name.
647658
var frameName = msg.frameID;
648659
var iframe = document.getElementsByName(frameName)[0];
@@ -660,9 +671,9 @@ var privly = {
660671

661672
//finds the link and displays it
662673
var link = iframe.nextSibling;
663-
if(link.getAttribute("data-privlyhref")==url){
674+
if(link.getAttribute("data-privlyhref") === url){
664675
//check if it is not already displayed
665-
if(link.getAttribute("data-privly-display")=="false"){
676+
if(link.getAttribute("data-privly-display") === "false"){
666677
link.setAttribute("data-privly-display","true");
667678
link.style.display = "inherit";
668679
}
@@ -686,17 +697,8 @@ var privly = {
686697

687698
"use strict";
688699

689-
//check the format of the message
690-
if (typeof(message.origin) !== "string"){
691-
return;
692-
}
693-
694700
var msg = JSON.parse(message.data);
695701

696-
if(msg.command != 'show'){
697-
return;h
698-
}
699-
700702
//Get the element by name.
701703
var frameName = msg.frameID;
702704
var iframe = document.getElementsByName(frameName)[0];
@@ -714,8 +716,8 @@ var privly = {
714716

715717
//finds the link and hides it
716718
var link = iframe.nextSibling;
717-
if(link.getAttribute("data-privlyhref")==url){
718-
if(link.getAttribute("data-privly-display")=="true"){
719+
if(link.getAttribute("data-privlyhref") === url){
720+
if(link.getAttribute("data-privly-display") === "true"){
719721
link.setAttribute("data-privly-display","false");
720722
link.style.display = "none";
721723
}
@@ -740,17 +742,8 @@ var privly = {
740742

741743
"use strict";
742744

743-
//check the format of the message
744-
if (typeof(message.origin) !== "string"){
745-
return;
746-
}
747-
748745
var msg = JSON.parse(message.data);
749746

750-
if(msg.command != 'remove'){
751-
return;
752-
}
753-
754747
//Get the element by name.
755748
var frameName = msg.frameID;
756749
var iframe = document.getElementsByName(frameName)[0];
@@ -762,9 +755,9 @@ var privly = {
762755

763756
//finds the link and displays it
764757
var link = iframe.nextSibling;
765-
if(link.getAttribute("data-privlyhref")==url){
758+
if(link.getAttribute("data-privlyhref") === url){
766759
//check if it is not already displayed
767-
if(link.getAttribute("data-privly-display")=="false"){
760+
if(link.getAttribute("data-privly-display") === "false"){
768761
link.setAttribute("data-privly-display","true");
769762
link.style.display = "inherit";
770763
}
@@ -856,25 +849,7 @@ var privly = {
856849
//The content's iframe will post a message to the hosting document.
857850
//This listener sets the height of the iframe according to the messaged
858851
//height
859-
window.addEventListener("message", privly.resizeIframePostedMessage,
860-
false, true);
861-
862-
//The content's iframe will post a message to the hosting document.
863-
//This listener sets the height of the iframe according to the messaged
864-
//height
865-
window.addEventListener("message", privly.showIframePostedMessage,
866-
false, true);
867-
868-
//The content's iframe will post a message to the hosting document.
869-
//This listener sets the height of the iframe according to the messaged
870-
//height
871-
window.addEventListener("message", privly.hideIframePostedMessage,
872-
false, true);
873-
874-
//The content's iframe will post a message to the hosting document.
875-
//This listener sets the height of the iframe according to the messaged
876-
//height
877-
window.addEventListener("message", privly.removeIframePostedMessage,
852+
window.addEventListener("message", privly.checkIframePostedMessage,
878853
false, true);
879854

880855
privly.runPending = true;

0 commit comments

Comments
 (0)