Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
df5b0824
Commit
df5b0824
authored
May 25, 2017
by
Robert Hodan
Committed by
Juan Corona
Jun 05, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add initial support for sub-frame injection
parent
2c4d883e
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
128 additions
and
0 deletions
+128
-0
main.js
src/annotator/main.js
+3
-0
cross-frame.coffee
src/annotator/plugin/cross-frame.coffee
+23
-0
frame-util.js
src/annotator/util/frame-util.js
+102
-0
No files found.
src/annotator/main.js
View file @
df5b0824
...
...
@@ -20,6 +20,7 @@ if (window.wgxpath) {
var
$
=
require
(
'jquery'
);
// Applications
var
Guest
=
require
(
'./guest'
);
var
Sidebar
=
require
(
'./sidebar'
);
var
PdfSidebar
=
require
(
'./pdf-sidebar'
);
...
...
@@ -46,6 +47,8 @@ $.noConflict(true)(function() {
Sidebar
;
if
(
config
.
hasOwnProperty
(
'constructor'
))
{
Klass
=
config
.
constructor
;
if
(
Klass
===
"Guest"
)
Klass
=
Guest
;
delete
config
.
constructor
;
}
...
...
src/annotator/plugin/cross-frame.coffee
View file @
df5b0824
...
...
@@ -3,6 +3,7 @@ Plugin = require('../plugin')
AnnotationSync
=
require
(
'../annotation-sync'
)
Bridge
=
require
(
'../../shared/bridge'
)
Discovery
=
require
(
'../../shared/discovery'
)
frameUtil
=
require
(
'../util/frame-util'
)
# Extracts individual keys from an object and returns a new one.
...
...
@@ -33,6 +34,11 @@ module.exports = class CrossFrame extends Plugin
bridge
.
createChannel
(
source
,
origin
,
token
)
discovery
.
startDiscovery
(
onDiscoveryCallback
)
# THESIS TODO: Disabled until fully implemented.
#
# Find, and inject Hypothesis into Guest's iframes
# _discoverOwnFrames()
this
.
destroy
=
->
# super doesnt work here :(
Plugin
::
destroy
.
apply
(
this
,
arguments
)
...
...
@@ -50,3 +56,20 @@ module.exports = class CrossFrame extends Plugin
this
.
onConnect
=
(
fn
)
->
bridge
.
onConnect
(
fn
)
_discoverOwnFrames
=
()
->
# Discover existing iframes
iframes
=
frameUtil
.
findIFrames
(
elem
)
_handleIFrames
(
iframes
)
_handleIFrame
=
(
iframe
)
->
if
!
frameUtil
.
isAccessible
(
iframe
)
then
return
if
!
frameUtil
.
hasHypothesis
(
iframe
)
frameUtil
.
injectHypothesis
(
iframe
)
_handleIFrames
=
(
iframes
)
->
for
own
index
,
iframe
of
iframes
_handleIFrame
(
iframe
)
src/annotator/util/frame-util.js
0 → 100644
View file @
df5b0824
'use strict'
;
// THESIS TODO: Once this module is finalized (more or less), then make
// sure to remove JQuery and refactor.
var
$
=
require
(
'jquery'
);
module
.
exports
=
{
findIFrames
,
hasHypothesis
,
injectHypothesis
,
isAccessible
,
isValid
,
};
// Find all iframes within this iframe only
function
findIFrames
(
iframe
)
{
var
iframes
=
[];
$
(
'iframe'
,
iframe
).
each
(
function
(
index
,
iframe
)
{
if
(
isValid
(
iframe
)
)
{
iframes
.
push
(
iframe
);
}
});
return
iframes
;
}
// Check if the iframe has already been injected
function
hasHypothesis
(
iframe
)
{
// THESIS TODO: Are there better identifiers to use?
var
scripts
=
{
src
:
[
'/hypothesis'
,
'/embed.js'
,
]
};
var
frameBody
=
iframe
.
ownerDocument
.
body
;
var
childNodes
=
frameBody
.
childNodes
;
var
hasHypothesis
=
false
;
for
(
var
i
=
0
;
i
<
childNodes
.
length
-
1
;
i
++
)
{
var
node
=
childNodes
[
i
];
if
(
node
.
tagName
!==
'SCRIPT'
)
continue
;
// Skip to next increment
for
(
var
j
=
0
;
j
<
scripts
.
src
.
length
-
1
;
j
++
)
{
var
src
=
scripts
.
src
[
j
];
if
(
node
.
src
.
includes
(
src
))
{
hasHypothesis
=
true
;
return
;
// Found our answer, stop looping.
}
}
}
return
hasHypothesis
;
}
// Inject embed.js into the iframe
function
injectHypothesis
(
iframe
,
i
)
{
if
(
!
iframe
)
return
;
var
iframes
;
// Support arrays via recursion
if
(
iframe
.
constructor
===
Array
)
{
if
(
!
iframe
.
length
)
return
;
iframes
=
iframe
;
i
=
(
i
!=
undefined
)
?
i
:
0
;
// if set, use i. Otherwise, use 0
iframe
=
iframes
[
i
];
}
var
config
=
document
.
createElement
(
'script'
);
config
.
className
=
"js-hypothesis-config"
;
config
.
type
=
"application/json"
;
config
.
innerText
=
' {"constructor": "Guest" }'
;
// THESIS TODO: Temporarily hardcoded
var
src
=
'http://localhost:3001/hypothesis'
;
var
embed
=
document
.
createElement
(
'script'
);
embed
.
async
=
true
;
embed
.
src
=
src
;
iframe
.
contentDocument
.
body
.
appendChild
(
config
);
iframe
.
contentDocument
.
body
.
appendChild
(
embed
);
if
(
iframes
&&
i
<
iframes
.
length
-
1
)
{
this
.
_injectHypothesis
(
iframes
,
++
i
);
}
}
// Check if we can access this iframe's document
function
isAccessible
(
iframe
)
{
try
{
iframe
.
contentDocument
;
return
true
;
}
catch
(
e
)
{
return
false
;
}
}
// Check if this is an iframe that we want to inject embed.js into
function
isValid
(
iframe
)
{
// Currently only checks if it's not the h-sidebar
return
(
iframe
.
className
!==
'h-sidebar-iframe'
);
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment